Object and class one object and class
- A class is a mold that determines the characteristics (attributes) and behavior (methods) that an object will have;
- A class is a set of objects of the same property and method;
- Class is the type of object;
1. Properties
A specific value or feature.
2. Methods
The action that the object performs is what it can do.
3. Defining classes
- Java programs use class classes as organizational units;
- Composition: Properties and methods;
4. The difference between a member variable and a local variable
- different scopes;
- The initial value is different; Java gives the member variable default an initial value, while the local variable does not;
- Local variable names with the same name are not allowed in the same method, but can be in different methods;
- The nearest principle; The local and the member have the same name, the value of the local variable is taken precedence;
5. Construction method
- Create an object using the new + construct method, which is the constructor of the calling class ;
- The constructor method name has the same name as the class and no return value;
- The construction method can also be overloaded;
6. Static modifier
You can modify variables, methods, and blocks of code. It is used by all objects of the class, and when the JVM first uses the class, it allocates memory until the class is unloaded for resource reclamation.
Static methods can call static members directly in the same class, but cannot call non-static members directly;
In the ordinary member method, the same non-static variables and static variables can be accessed directly;
Non-static methods cannot be called directly in static methods, and non-static methods need to be accessed through objects.
Static initialization blocks are only executed when the class is loaded and only once, while static initialization blocks can only assign values to static variables and cannot initialize normal member variables; View the following code:
Operation Result:
by outputting The result, we can see that the static initialization block is executed first when the program is run, then the normal initialization block is executed, and the construction method is finally executed. Because static initialization blocks are only executed once when the class is loaded, static initialization blocks are not executed when the object Hello2 is created again.
Two-pack
Hiding some information about a class inside a class, not allowing direct access to external programs, but using the methods provided by the class to manipulate and access the hidden information.
1. Access modifiers
2. This keyword
Chapter Nineth Objects and classes