As we know, classes are the basic elements of Java. A java program is composed of several different classes. The keyword is class and the format is
class Class name{ Content of class body }
The class body consists of two parts:
Declaration of variables and definition of methods
variable
Format: data type variable name;
The variable type of a member can be any kind of data type, such as basic types such as short, int, long, float, double, char, boolean, and reference types such as arrays, objects, and interfaces.
constant
Keyword: final
If the member variable and local variable are modified by the keyword final, the variable is a constant. The program must assign a value when declaring the constant, and the value will not change during operation, otherwise the program will report an error.
class Class{ final int PI 3,1415926;//Indicates that the value of PI is always 31415926 }
Static variable
Keyword: static
When a variable is modified by static, it will be owned by the class but not by the object. When we call it, we can call it with the object or directly with the class name, but usually it is called with the class name, and the value of the static variable can be rewritten when called by the object, for example:
public class My_Idol{ //Define a static variable staitc String idol="Li Xian"; //Main method public static void main(String[] args){ //Create two or three objects My_Idol shiye1=new My_Idol(); My_Idol shiye2=new My_Idol(); My_Idol shiye3=new My_Idol(); //Call static variables directly using the class name System.out.println("Called by class name:"+My_idol.idol); //Using object calls System.out.println("Call with object 1:"+shiye1.idol); System.out.println("Call with object 2:"+shiye2.idol); } }
The running result of the program is:
Call by class name: Li Xian Calling with object 1: Li Xian Calling with object 2: Li Xian
When we modify the value of a static variable with an object:
public class My_Idol { //Define a static variable static String idol="Li Xian"; //Main method public static void main(String[] args){ //Create two objects My_Idol shiye1=new My_Idol(); My_Idol shiye2=new My_Idol(); //Modify the value of static variable with object 2 shiye2.idol="Pu Yixing"; //Call static variables directly using the class name System.out.println("Called by class name:"+My_Idol.idol); //Using object calls System.out.println("Call with object 1:"+shiye1.idol); //Called with the modified value System.out.println("The modified value is:"+shiye2.idol); } }
The results after the program is run are:
*Called by class name:Pu Yixing Call with object 1:Pu Yixing The modified value is:Pu Yixing
Through two examples, we can see that although different objects are created, the results are the same when they call static variables, and when the value of static variables is modified through one object, other objects call the modified value, which shows that static variables are unique in the class and belong to the class rather than the object, So usually, we call static variables directly with the class name.
static const
Format: static final data type constant name = a value
class{ static final String idol="Li Xian"; }
Construction method
A constructor is a special method in a class that can only be called when an object is created. The constructor name must be the same as the class name and have no type. We can write several construction methods in the class, but the parameters (number, type, etc.) of these methods must be different. If it is not written, the system will default to a construction method, which has no parameters and no statements in the method. Examples are as follows:
public class My_Idol { //Construction method 1 My_Idol(String n){ System.out.println("My love bean is"+n); } //Construction method 2 My_Idol(int n){ System.out.println("I have"+n+"A love bean"); } //Main function public static void main(String[] args) { //The constructor is called directly when the object is created My_Idol my1=new My_Idol("Li Xian"); My_Idol my2=new My_Idol(2); } }
The result of program running is:
My love bean is Li Xian I have two love beans
The above program creates two construction methods. Their parameters have different data types (also known as method overloading) and are called directly when creating objects, which is also called passing parameters with constructors.
Methods with null return value and methods with return value
public class Add{ //Method without return value void Add1(int x,int y) { int z; z=x+y; System.out.println("x+y="+z); } //Method with return value int Add2(int x,int y){ int z; z=x+y; System.out.println("x+y="+z); return z; } //Main function public static void main(String[] args) { Add adds=new Add(); adds.Add1(10, 20); adds.Add2(30, 40); } }
The operation result is:
x+y=30 x+y=70
final method
When a method is final ly modified, subclasses are not allowed to override it during inheritance.
Static method
Static methods are similar to static variables.
Static methods belong to classes and can be called directly with class names;
Static methods can call static variables directly. If you want to call non static variables, you can only create objects;
Static methods can also call static methods directly. If you want to call non static methods, you can only call them by creating objects. Examples are as follows
public class Add { //Define a static variable and a non static variable static int x=10; int y=10; //Create a static method static void Add1() { //Use static variable x directly System.out.println("x="+x); //You must create an object before calling through the object Add add=new Add(); System.out.println("y="+add.y); } //Main function public static void main(String[] args) { //Because the main function is also a static function, you can directly call the static function Add1 Add.Add1(); } }
The running result of the program is:
x=10 y=10
Code block
Common code block:
Is the method body in the method
Initialize code block:
Executes when an object is created
Static code block
Called only once and executed at load time
Examples are as follows:
public class Add { { System.out.println("Code block"); } static { System.out.println("Static code block"); } //Main function public static void main(String[] args) { Add a=new Add(); Add b=new Add(); } }
Operation results:
Static code block Code block Code block
We can find that after the object is created several times, the code runs several times quickly, while the static code block runs only once and is executed before the object is created.
Main method
The main method is used too much, so don't mention it
public static void main(String[] args){ }
There can also be many components in a class. Here are several common ones. Readers can find out what else they have~