What are Classes and Objects?
A class is a template for objects, and an object is an instance of a class.
Static vs. Public
we created a static
method, which means that it can be accessed without creating an object of the class, unlike public
, which can only be accessed by objects
public class Main { // Static method static void myStaticMethod() { System.out.println("Static methods can be called without creating objects"); } // Public method public void myPublicMethod() { System.out.println("Public methods must be called by creating objects"); } // Main method public static void main(String[] args) { myStaticMethod(); // Call the static method // myPublicMethod(); This would compile an error Main myObj = new Main(); // Create an object of Main myObj.myPublicMethod(); // Call the public method on the object } }
Modifiers
The public
keyword is an access modifier
public class Main
Access Modifiers
For classes:
public: The class is accessible by any other class
default: The class is only accessible by classes in the same package. This is used when you don't specify a modifier.
For attributes, methods and constructors:
public: The code is accessible for all classes
private: The code is only accessible within the declared class
default: The class is only accessible by classes in the same package. This is used when you don't specify a modifier.
protected: The code is accessible in the same package and subclasses
Non-Access Modifiers
For classes:
final: The class cannot be inherited by other classes
abstract: The class cannot be used to create objects
For attributes and methods:
final: Attributes and methods cannot be overridden/modified
public class Main { final int x = 10; final double PI = 3.14; public static void main(String[] args) { Main myObj = new Main(); myObj.x = 50; // will generate an error: cannot assign a value to a final variable myObj.PI = 25; // will generate an error: cannot assign a value to a final variable System.out.println(myObj.x); } }
static: Attributes and methods belongs to the class, rather than an object
abstract: Can only be used in an abstract class, and can only be used on methods. The method does not have a body, for example abstract void run();. The body is provided by the subclass (inherited from).
// Code from filename: Main.java // abstract class abstract class Main { public String fname = "John"; public int age = 24; public abstract void study(); // abstract method } // Subclass (inherit from Main) class Student extends Main { public int graduationYear = 2018; public void study() { // the body of the abstract method is provided here System.out.println("Studying all day long"); } } // End code from filename: Main.java // Code from filename: Second.java class Second { public static void main(String[] args) { // create an object of the Student class (which inherits attributes and methods from Main) Student myObj = new Student(); System.out.println("Name: " + myObj.fname); System.out.println("Age: " + myObj.age); System.out.println("Graduation Year: " + myObj.graduationYear); myObj.study(); // call abstract method } }
transient: Attributes and methods are skipped when serializing the object containing them
synchronized: Methods can only be accessed by one thread at a time
volatile: The value of an attribute is not cached thread-locally, and is always read from the "main memory"
Encapsulation
The meaning of Encapsulation, is to make sure that "sensitive" data is hidden from users. To achieve this, you must:
- declare class variables/attributes as
private
- provide public get and set methods to access and update the value of a
private
variable
Scanner
Using the Scanner
class to get user input:
import java.util.Scanner; class MyClass { public static void main(String[] args) { Scanner myObj = new Scanner(System.in); System.out.println("Enter username"); String userName = myObj.nextLine(); // get user's input when user press on Enter System.out.println("Username is: " + userName); } }
Inheritance
class Vehicle { protected String brand = "Ford"; // Vehicle attribute public void honk() { // Vehicle method System.out.println("Tuut, tuut!"); } } class Car extends Vehicle { private String modelName = "Mustang"; // Car attribute public static void main(String[] args) { // Create a myCar object Car myCar = new Car(); // Call the honk() method (from the Vehicle class) on the myCar object myCar.honk(); // Display the value of the brand attribute (from the Vehicle class) and the value of the modelName from the Car class System.out.println(myCar.brand + " " + myCar.modelName); } }
We set the brand attribute in Vehicle to a protected
access modifier. If it was set to private
, the Car class would not be able to access it.
Why And When To Use "Inheritance"?
- It is useful for code reusability: reuse attributes and methods of an existing class when you create a new class.
! cannot inherit from final Class
Polymorphism
class Animal { public void animalSound() { System.out.println("The animal makes a sound"); } } class Pig extends Animal { public void animalSound() { System.out.println("The pig says: wee wee"); } } class Dog extends Animal { public void animalSound() { System.out.println("The dog says: bow wow"); } } class Main { public static void main(String[] args) { Animal myAnimal = new Animal(); // Create a Animal object Animal myPig = new Pig(); // Create a Pig object Animal myDog = new Dog(); // Create a Dog object myAnimal.animalSound(); myPig.animalSound(); myDog.animalSound(); } } // Outputs: The animal makes a sound The pig says: wee wee The dog says: bow wow
标签:Java,void,System,public,Classes,println,Main,class From: https://www.cnblogs.com/ShengLiu/p/16928928.html