I. Static member variables
1. belongs to the entire class instead of an object instance, so it can be called directly from the class name and object name.
2. Static members belong to the entire class, and when the system first uses the class, it allocates memory space until the class is unloaded for resource recycling
Two. Static methods
1. Static methods can call static members directly in the same class, but not non-static members, or if you want to invoke non-static variables in a static method, you can access non-static variables by creating the object of the class and then through the object
2. in the ordinary member method, you can directly access the same non-static variables and static variables
3. non-static methods cannot be called directly in static methods, and non-static methods need to be accessed through objects
Note: A static method belongs to a class, and memory must allocate memory space for it, which is always occupied by a static method, and the memory manager does not retract the storage space of the static method because the static method is not called, so that if all the methods are declared as static methods, it consumes a lot of memory space , and finally the system slows down. While the normal member method is called by the object, memory is not always allocated memory, only when the call allocates storage space, and when it is not called, storage space will be recalled by the memory management, freeing the unused space, improve the operating rate of the system
Three. Initializing blocks and static initialization blocks
1. Why do I have to initialize?
A: The initialization block is the addition of the constructor, the initialization block is not able to receive any parameters, the definition of some of the common properties of all objects, methods and so on, can be initialized with the initialization block
The advantage is that it can improve the reuse of the initialization block and improve the maintainability of the whole application.
2. Is there a difference between an initialization block and a static initialization block?
A: (1) when the program runs, the static initialization block is executed first, then the normal initialization block is executed, and the construction method is finally executed.
(2) Static initialization blocks are executed only once when the class is loaded, and only static member variables can be initialized, and normal variables cannot be initialized
Note: There are no static constructors in Java that perform similar functions with static blocks of code
1 public class HelloWorld {
2
3String name;//declaring variable name4String sex;//Declaring variables Sex5 Static intAge//declaring a static variable age6 7 //Construction method, the system will no longer automatically generate an argument-free construction method after the constructor method is declared manually, regardless of whether it declares a constructor with or without a parameter8 PublicHelloWorld () {9SYSTEM.OUT.PRINTLN ("Initialize name by construction method");TenName = "Tom"; One } A - //Initialize block - { theSYSTEM.OUT.PRINTLN ("Initialize sex by initialization block"); -Sex = "male"; - } - + //static initialization blocks, initialized only once when the class is loaded, and can only initialize static member variables, cannot initialize ordinary variables - Static { +SYSTEM.OUT.PRINTLN ("Initialize age by static initialization block"); AAge = 20; at } - - Public voidShow () { -System.out.println ("Name:" + name + ", Gender:" + Sex + ", Age:" +Age ); - } - in Public Static voidMain (string[] args) { - to //Creating Objects +HelloWorld Hello =NewHelloWorld (); - //call the Show method of an object the hello.show (); *HelloWorld hello1 = new HelloWorld (); $ }Panax Notoginseng}
Here is the output:
Initializing the Age by initializing a block initializes the sex through a static initialization block initialize name by constructor: Tom, Sex: Male, Age: 20 Initialize the sex by initializing the block initialize the name with the constructor method
Static member variables and static methods in Java classes are briefly introduced and continuously supplemented