[Java entry notes] Object-Oriented Programming BASICS (III): member variables and local variables, java Object-Oriented Programming

Source: Internet
Author: User

[Java entry notes] Object-Oriented Programming BASICS (III): member variables and local variables, java Object-Oriented Programming

In a class, variables can be divided into member variables and local variables according to their defined locations.

A member variable is a variable defined outside the method in the class body. A local variable is a parameter defined in the method.

Member variables

Member variables are dividedClass attributesAndInstance attributesThe instance attribute is not modified in static mode, but is called a class attribute. The difference between a class attribute and an instance attribute is that a class attribute belongs to a class, it does not belong to any object. It exists from the preparation stage of the class until the class is completely destroyed. The instance property is an object. It is created only when an object is created. The object is recycled and the instance property of the object is destroyed.

Definition Syntax of class attributes:
[Modifier] static data type variable name [= value];

The modifier can be public, protected, private, or not written.

The data type can be basic data type or reference data type.

Static is a modifier required for modifying class attributes.

Variable names are defined by themselves, but they must follow the identifier definition rules.

You can set a default value at the beginning, or you can leave it empty. If you leave it blank, the system will assign a default value.

Byte, short, int, and long are 0 by default.

Double and float are 0.0 by default.

Char: \ u0000 by default'

Boolean is false by default.

The default reference type is null.

 

Access:

Class Name. Class Name

 

Of course, you can also use the object name category class attribute in Java, but this is not recommended, because logically, the class attribute belongs to the class and should not be accessed using the attribute.

 

Definition Syntax of instance properties:
[Modifier] data type variable name [= value];

 

The rule is similar to a class attribute, but does not have static modification.

Access:

Object Name. attribute name

 

 

Example:
Public class Person {// defines two instance attributes: public String name; public int age; // defines two class attributes and sets the default value public static int eyeNum = 2; public static int mouthNum = 1 ;}

 

Access

Public class Test {public static void main (String [] args) {Person p = new Person (); p. name = "James"; p. age = 20; System. out. println ("My name is:" + p. name); System. out. println ("I have" + Person. mouthNum + "Mouth"); Person p2 = new Person (); p2.name = "Li Si"; p2.age = 25; System. out. println ("My name is:" + p2.name); System. out. println ("I have" + Person. mouthNum + "Mouth ");}}

 

 

 

 

Output after running:

My name is: James
I have 1 mouth
My name is: Li Si
I have 1 mouth

Demo of creating and using instance attributes and class attributes in memory

As we mentioned earlier, class attributes are loaded into the memory when the class is ready to be loaded and stored in the data segment. They exist after the class is loaded.

When our code is executed to Person p = new Person (); when an object is created, instance variables are created for the object. If no default value is assigned, the default value is assigned to the system:

As we can see from the above, although eyeNum and mouthNum belong to the class attributes of the Person class, the p object can still find the class attributes through reference, but we mentioned above, we do not recommend that you use an object to obtain or modify class attributes.

We can set or obtain the name and age attributes through the p object.

Name is an immutable String that references the Data Type String. Let's look at it in detail later in the blog. You are welcome to look around it. It is usually like this in the memory.

 

Continue to create 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:

Therefore, changing p2 will not affect the value of the p object. But what if we modify the value of the class attribute?

In the preceding example, we add the following code:

Public class Test {public static void main (String [] args) {Person p = new Person (); p. name = "James"; p. age = 20; // System. out. println ("My name is:" + p. name); // System. out. println ("I have" + Person. mouthNum + "Mouth"); Person p2 = new Person (); p2.name = "Li Si"; p2.age = 25; // System. out. println ("My name is:" + p2.name); // System. out. println ("I have" + Person. mouthNum + "Mouth"); // newly added code, modifying the class attribute value p. mouthNum = 2; System. out. println ("My name is:" + p. name); System. out. println ("I have" + p. mouthNum + "Mouth"); System. out. println ("My name is:" + p2.name); System. out. println ("I have" + p2.mouthNum + "Mouth ");}}

 

 

Output:

My name is: James
I have 2 mouths
My name is: Li Si
I have 2 mouths

 

We can see that the new Code uses the object name. the class property method is the class property, and the result is a value. You can also read the book. The class property belongs to the class, and modifying the class property affects each other.

Local variable

There are three types of local variables:

Parameters: The parameter value required by the method when defining the method, which is valid throughout the method.

Method local variable: Variables defined in the method body are valid from the beginning of the variable definition to the end of the method.

Code block local variable: Variables defined in the Code block are valid at the beginning of the variable definition and the end of the code block. For example, loop or variable defined in judgment

Example:

Public class Test {public static void main (String [] args) {new Test (). printHello (2);} public void printHello (int i1) {// i1 is a form parameter, which has been valid for int i2 = 1 throughout the method execution; // i2 is a local variable of the method. It is valid until the end of the method. while (i2 <= i1) {int i3 = 1; // i3 is a local variable of the code block, starting from here to the end of the loop body. out. println ("Hello"); i3 ++; i2 ++ ;}}}

 

 

 

Local variable creation and usage in memory

When new Test (). printHello (2); is executed, call the printHello method and input a parameter:

Run the following command: int i2 = 1;

Execute downward and Judge cyclically. If i2 is smaller than i1, enter the loop and execute int i3 = 1;

Continue execution, output, i2, i3 auto-increment 1

Statement block ends, i3 is cleared, and the loop statement is determined. i2 <= i1 meets the conditions and continues to enter the loop body.

Run down, define i3 again, and assign a value of 1

Continue to run down. Output: i2, i3 auto-increment 1

Judge the loop again. The condition is not true and the loop ends. Run the following command to clear all the variables in the method.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.