Java basic learning day38 (static, re-recognize the main() method)

1. static

Static means static, which is a modifier in java, which can modify member methods and member variables

  • Member variables modified by static are called static variables

Features:
a. Shared by all objects of this class, all objects have one assignment, and all objects can change it.
b. Belongs to the class, not the object
c. Loads with the loading of the class, which is better than the existence of the object

Call method:
a. Class name call (recommended)
b. Object name call

  • Member methods modified by static are called static methods

Features:
a. Mostly used in test classes and tool classes
b. Rarely used in javabean classes

Call method:
a. class name call
b. object name call

2. Static memory map
insert image description here
insert image description here

Static area:
a. Before JDK8, it was in the method area. Starting from JDK8, it was placed in the heap space
b. Store all static variables of this class, no static methods (static methods and non-static methods are temporary Stored in the method area/meta space, wait until the statement is executed, and then load it into the stack)
c. After the bytecode file of this class is loaded into the method area, the static area appears, and the static variable also appears
d . Through the object instance of this class, you can find the static area corresponding to this class

3. Javabean class, test class, tool class

Javabean class:
a. A class used to describe a class of things, such as: Student, Teacher, Dog, Cat, etc.
b. First privatize member variables, then create empty parameters and constructors with all parameters, and then privatize each The member variables provide get and set methods, and finally write the corresponding member methods for additional behaviors

Test class:
a. It is used to check whether other classes are written correctly. The class with the main method is the main entry point of the program.
b. For example, create an object of the javabean class and make an assignment call

Tool class:
a. It is not used to describe a class of things, but a class that helps us do something
b. The name of the class is well-known
c. The private constructor does not allow the outside world to create objects, because its objects have no meaning
d. The method is defined as static, easy to call

4. Precautions for static

  • Static methods can only access static variables and static methods
  • Non-static methods can access static variables or static methods, and can also access non-static member variables and non-static member methods (non-static member variables are also called instance variables)
  • There is no this keyword in static methods
  • Among the formal parameters of the non-static method, there is a hidden this whose type is the same as that of the method caller
  • There is also a hidden this before the non-duplicate member variable in the non-static method
  • The non-static method (static method) in the class is called in the non-static method. There is also a hidden this before the non-static method (static method). There are two ways to call the static method (variable): class name and object name

a. this represents the address value of the current method caller
b. this this is automatically assigned by the virtual machine and cannot be assigned manually

5. Re-recognize the main() method

public static void main ( String[ ] args ) {

}

  • public: Called by the JVM and given to public, other classes can also use this method directly (the main() method is called by the virtual machine, and the access rights need to be large enough)
  • static: Called by the JVM, without creating an object, and directly accessing the class name
    , because the main method is static, so other methods in the test class also need to be static.
  • void: called by the JVM, no need to return a value to the JVM
  • main: a popular name, although it is not a keyword, it is recognized by the JVM
  • String[ ] args: Used to receive keyboard input data, but now it is useless

Guess you like

Origin blog.csdn.net/u011453680/article/details/128879502