In a class, variables can be divided into member variables and local variables, depending on where they are defined.
A member variable is a variable defined outside a method within a class, and a local variable is a parameter defined in a method.
Member variables
Member variables are divided into class and instance properties, which are defined without static adornments called instance properties, which are called class properties with static adornments, and the difference between class properties and instance properties is that the class property belongs to the class and does not belong to which object. It has been in existence since the preparatory stage of this class, until this class is completely destroyed. An instance property belongs to an object, and it is created only when an object is created, the object is recycled, and the instance property belonging to the object is destroyed.
The definition syntax for class properties:
static data type variable name [= value];
Modifiers can be public, protected, private, and non-writable
Data types can be basic data types and reference data types
Static is the modifier that is required to modify the class property
Variable names are defined by themselves, but follow the identifier definition rules
Value can be set at the beginning of a default value, or you can not fill out the system will be assigned a default value
BYTE, short, int, long default to 0
Double, float defaults to 0.0
char defaults to ' \u0000 '
Boolean defaults to False
Reference type defaults to NULL
Access:
Class name. Class Property Name
Of course, you can also use object names to access class properties in Java, but this is not recommended because, logically, a class property belongs to a class and should not be accessed with a property.
The definition syntax for instance properties:
[modifier] Data type variable name [= value];
Rules are similar to class properties, except that static adornments are not.
Access:
Object name. Property name
Cases:
public class person{ // Define two instance properties public String name; public int age; // Define two class properties and set default values for them public static int eyenum = 2 ; public static int mouthnum = ;}
Access
Public classtest{ Public Static voidMain (string[] args) {person P=NewPerson (); P.name="Zhang San"; P.age= -; System. out. println ("My name is:"+p.name); System. out. println ("I have"+ Person.mouthnum +"a mouth"); Person P2=NewPerson (); P2.name="John Doe"; P2.age= -; System. out. println ("My name is:"+p2.name); System. out. println ("I have"+ Person.mouthnum +"a mouth"); }}
Post-run output:
My name is: Zhang San
I have 1 mouths.
My name is: John Doe
I have 1 mouths.
Instance properties and class properties are created and used in in-memory demos
As we said earlier, class properties are loaded into memory when the class is ready to load, stored in the data segment, and they exist after the class is loaded.
When our code executes to person P = new person (); Creating an object creates a good instance variable for the object, and the system assigns a default value if no default is assigned:
As we can see above, although Eyenum and Mouthnum belong to the class property of the person class, the P object can still find the class property by reference, but as we mentioned earlier, it is not recommended to use objects to get or modify class properties.
We can set or get the name and age properties through the P object
Name is an immutable string reference data type string, after the blog we look at it in detail, welcome everyone to watch, it is now in memory roughly like this.
Continue creating the P2 object:
We can see that no matter how many person objects are created, there is only one eyenum and Mouthnum.
Assign a value to P2:
So, when changing P2, it doesn't affect the value of the P object, but what if we modify the value of the Class property?
Let's add a few sentences to the example above:
Public classtest{ Public Static voidMain (string[] args) {person P=NewPerson (); P.name="Zhang San"; P.age= -; //System.out.println ("My name is:" + p.name); //System.out.println ("I have" + Person.mouthnum + "a Mouth");Person P2=NewPerson (); P2.name="John Doe"; P2.age= -; //System.out.println ("My name is:" + p2.name); //System.out.println ("I have" + Person.mouthnum + "a Mouth"); //new code, modifying the value of the class propertyP.mouthnum =2; System. out. println ("My name is:"+p.name); System. out. println ("I have"+ P.mouthnum +"a mouth"); System. out. println ("My name is:"+p2.name); System. out. println ("I have"+ P2.mouthnum +"a mouth"); }}
Output:
My name is: Zhang San
I have 2 mouths.
My name is: John Doe
I have 2 mouths.
We can see that the new code we use the object name. class properties to access the class properties, get a value, so you can also read books, class attributes belong to the class, modify the class properties, there is an impact on each other.
Local variables
Local variables can be divided into three types:
formal parameters: The parameter values required by the method when defining the method are valid throughout the method.
method Local Variables : variables defined in the method body, valid at the beginning of the variable definition and at the end of the method.
code block local variable : A variable defined in a code block that is valid at the beginning of a variable definition to the end of a block of code. For example, a variable defined in a loop or judgment
Cases:
Public classtest{ Public Static voidMain (string[] args) {NewTest (). Printhello (2); } Public voidPrinthello (intI1) {//I1 is a formal parameter that is valid throughout the execution of the method intI2 = 1;//I2 is a method local variable, starting here, to the end of the method has been valid while(I2 <=I1) {inti3 = 1;//i3 is a code block local variable, starting here, to the end of the loop body is validSystem.out.println ("Hello"); i3++; I2++; }}}
Local variable creation and use in-memory demo
When executed to new Test (). Printhello (2); , call the Printhello method and pass in a parameter:
Execute down: int i2 = 1;
Execution down, loop judgment, i2 less than I1, enter the loop, execute int i3 = 1;
Continue execution, output, I2,i3 self-add 1
Statement block end, i3 is cleared, continue to judge the Loop statement, I2<=I1 meet the conditions, continue to enter the loop body
Execute down, define i3 again, assign a value of 1
Continue execution down, output, i2,i3 add 1
Again judge the loop, the condition is not set, the cycle ends. Executes down, the method ends, and all the variables in the method are emptied.
[Java Getting Started note] Object-oriented programming Fundamentals (III): member variables and local variables