member variables and local variables

copy code
class Student
{

    String name;//Private
    

    Student(){}

    Student(String n)
    {
        name = n;
    }


    //No tuition fee paid by default
    boolean isFees = false;
    
    //pay tuition
    void fees()
    {
        isFees = true;//Modify the status that the student has not paid tuition
    }

    void show()
    {    
        //String name = "OOXX";
        System.out.println(name +"-->" + isFees);
    }
}

class StudentDemo
{
    public static void main(String[] args)
    {
        
        Student s1 = new Student("Will");
        Student s2 = new Student("Lucy");
        
        s1.show();// will  false
        s2.show();//Lucy   false

        //Create an array for students who have not paid tuition fees

        Student[] unFees = new Student[]{s1,s2};

        // Take out each student in the array and call their behavior of paying tuition separately
        for(Student s :  unFees)
        {
            s.fees();//Students pay tuition
        }
        
        //========================================
        s1.show();// will  true
        s2.show();//Lucy   true

        
            
    }
}
copy code

Member variables:

  (field) instance field (no static modification)

    Class field (static modification)

local variable:

  formal parameter

  The method local variable is defined without an initial value, which is fine, but it cannot be used. To use it, an initial value must be given.

  code block local variable {}

 

The first letter of the variable name is lowercase, and if it consists of multiple words, the first letter of the other words starting from the second word is uppercase.

If the name of the local variable is the same as the name of the member variable, to use the member variable in the method, you must use the keyword this

 

copy code
1 class People {  
 2 String name = "name of class redefined"; //Member variables in the class and assignment  
 3   
 4     People(){  
 5     }  
 6       
 7     public void speak() {  
 8 String name = "name defined in the class body method"; //Define the name with the same name as the member variable in the method speak and assign it  
 9         System.out.println(name);  
10 System.out.println(this.name); //Access member variables in the class through this  
11           
12     }  
13 }  
14   
15   
16 public class TestThis { //There can only be one class in the source file that is a public class, and the name of the source file must be exactly the same as the name of this class, such as  
17 //If there is no public class, then the name of the source file only needs to be the same as the name of a class.  
18     public static void main(String[] args) {         
19         People myPeople = new People();  
20         myPeople.speak();  
21     }  
22   
23 }  
copy code

Difference between member variable and local variable

 

       Member variables:

          1. Member variables are defined in the class and can be accessed throughout the class.

          2. Member variables are established with the establishment of the object, and disappear with the disappearance of the object, and exist in the heap memory where the object is located.

          3. Member variables have default initialization values.

      local variable:

          1. Local variables are only defined in the local scope, such as: functions, statements, etc., and are only valid in the area to which they belong.

          2. Local variables exist in the stack memory, the scope of the function ends, and the variable space will be automatically released.

          3. Local variables do not have default initialization values 

      The principles that need to be followed when using variables are: the principle of proximity

      First, look for it in the local scope, and use it if you have it; then look for it in the member position.

 

Difference between member variable and class variable

A variable modified by static is called a static variable, which is essentially a global variable. If a certain content is shared by all objects, then the content should be statically decorated; the content that is not statically decorated is actually a special description of the object.

The instance variables of different objects will be allocated different memory spaces. If the member variables in the class have class variables, then the class variables of all objects are allocated to the same memory. Changing the class variable of one of the objects will affect the This class variable of other objects, that is to say, the object shares the class variable.

copy code
1 class MyAdd {  
 2 int count = 0; //member variable counter  
 3 static int sum = 0; //Static variable counter  
 4     String name;  
 5       
 6   
 7     MyAdd(String name){  
 8         this.name = name;  
 9     }  
10       
11     public void myAddMethod() {    
12         count++;  
13 System.out.println(name+"value after calling member variable:"+count);  
14     }  
15       
16     public void staticAddMethod() {    
17         sum++;  
18 System.out.println(name+"The value of the variable after calling the class: "+sum);  
19     }  
20       
21       
22 }  
23   
24   
25 public class TestThis {  
26   
27     public static void main(String[] args) {  
28         MyAdd add1 = new MyAdd("add1");  
29         MyAdd add2 = new MyAdd("add2");  
30           
31         add1.myAddMethod();  
32         add2.myAddMethod();  
33         add1.myAddMethod();  
34         add1.staticAddMethod();  
35         add2.staticAddMethod();  
36         add1.staticAddMethod();  
37     }  
38   
39 }  
copy code

The difference between member variables and class variables:

   1. The life cycle of the two variables is different

      Member variables exist as objects are created and are released as objects are recycled.

      Static variables exist as classes are loaded and disappear as classes disappear.

   2. Different calling methods

      Member variables can only be called by objects.

      Static variables can be called by the object, but also by the class name.

   3. Different aliases

      Member variables are also known as instance variables.

      Static variables are also known as class variables.

   4. Different data storage locations

      Member variables are stored in objects in heap memory, so they are also called object-specific data.

      The static variable data is stored in the static area of ​​the method area (shared data area), so it is also called the shared data of the object.

 

 

 

static: ★★★ keyword, is a modifier used to modify members (member variables and member functions).

   Features:

   1. You want to achieve object sharing of common data in objects. This data can be statically modified.

   2. The statically modified members can be called directly by the class name. In other words, there is one more way to call static members. classname.Static method.

    3. Static is loaded as the class is loaded. And it takes precedence over object existence.

 

Disadvantages:

   1. Some data are object-specific data and cannot be statically modified. Because then, the unique data becomes the shared data of the object. That's the problem with the description of things. Therefore, when defining static, it must be clear whether this data is shared by objects.

   2. Static methods can only access static members, not non-static members.

      Because the static method is loaded before the object exists, there is no way to access the members in the object.

   3. The this and super keywords cannot be used in static methods.

      Because this represents an object, and when static is present, there may be no object, so this cannot be used.

 

When should static members be defined? In other words: when defining members, do they need to be statically modified?

There are two types of members:

   1. Member variables. (Static when data is shared)

      Whether the data of this member variable is the same for all objects:

      If it is, then the variable needs to be statically decorated because it is shared data. 

      If not, then it is said that this is the specific data of the object and should be stored in the object. 

   2. Member functions. (It is defined as static when no specific data is called in the method)

      If you judge whether the member function needs to be statically modified?

      Just for reference, whether the specific data in the object is accessed within the function:

      If there is access-specific data, the method cannot be statically decorated.

      If no specific data has been accessed, then this method needs to be statically decorated.

 

The difference between member variable and static variable:

   1. The object to which the member variable belongs. So it is also called instance variable.

      The class to which the static variable belongs. So it is also called a class variable.

   2. Member variables exist in heap memory.

      Static variables exist in the method area.

   3. Member variables exist as objects are created. Disappears as the object is recycled.

      Static variables exist as classes are loaded. Disappears with the disappearance of the class.

   4. Member variables can only be called by objects.

      Static variables can be called by objects or by class names.

   Therefore, member variables can be called object-specific data, and static variables are called object shared data.

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325122119&siteId=291194637