Recently watching Java video learning, make a small summary.
The difference between a member variable and a local variable:
1 : Member variables are defined directly in the class.
a local variable is defined in a method, on a parameter, in a statement.
2 : Member variables are valid in this class.
A local variable is valid only within the curly braces it belongs to, the curly brace ends, and the local variable loses scope.
3 : Member variables exist in heap memory and disappear as objects are created.
A local variable exists in the stack memory and is released as the owning region runs and ends.
--------------------------------------------------------------------------------------------
Package (one of the object-oriented features) : refers to the properties and implementation details of hidden objects and provides public access only externally.
Benefits: Isolation of changes, ease of use, improved reusability, and security.
Encapsulation principle: to hide the content that does not need to be provided externally, to hide the attributes, and to provide public methods to access them.
This : represents an object. is the reference to the object to which the function belongs.
What does this mean? which object invokes the function of this, which represents which object, which is the reference to which object.
When do you use this when developing?
When defining a feature, this is used to represent the object if it is used internally to the object that invokes the feature.
This can also be used to construct calls between functions.
Call format: This (actual parameter);
The This object follows . The member properties and member methods are called (the General method);
The This object is followed by the () constructor that calls the corresponding parameter in this class.
Note: The constructor is called with this and must be defined in the first row of the constructor. Because constructors are used for initialization, the initialization action must be performed. Otherwise, the compilation fails.
1 , the member variable. ( data sharing is static )
Whether the data for this member variable is the same for all objects:
If so, then the variable needs to be statically decorated because it is a shared data.
If not, then say that this is the object's unique data to be stored in the object.
2 , the member function. ( defined as static when no unique data is called in the method )
What if the member function needs to be statically decorated?
For reference, the function has access to the unique data within the object:
If there is access to unique data, the method cannot be statically decorated.
If no unique data has been accessed, then this method needs to be statically modified.
The Java Video Tutorial I read emphasizes the difference between a member variable and a static variable:
1, the member variable belongs to the object. So also known as instance variables.
Static variables belong to the class. So it is also called a class variable.
2, the member variable exists in heap memory.
Static variables exist in the method area.
3, member variables exist with the creation of the object. Disappears as the object is recycled.
Static variables exist as classes are loaded. Disappears as the class disappears.
4, member variables can only be called by the object.
A static variable can be called by an object or by a class name.
Therefore, member variables can be called unique data for objects, and static variables are called shared data for objects.
Static Note: The static life cycle is very long.
Static code block: a code block area with a static keyword. defined in the class.
Function: The initialization of the class can be completed. A static block of code executes as the class is loaded and executes only once (new objects are executed only once). If the main function is in the same class, it takes precedence over the main function execution.
Summary of variables in Java