The difference between a member variable and a local variable
1. Different positions in the class:
Member variables: Defined outside the method in the class.
Local variables: On a method declaration or in a method definition.
2. Different locations in memory:
Member variable: heap memory.
Local variables: Stack memory.
3. Different life cycle:
Member variables: Loaded as the class is loaded, and disappears as the class disappears.
Local variables: As the method call exists, disappears as the method call finishes.
4, the initialization value is different:
Member variable: initialized by default by the system. For member variables, you can not give the initialization value, which is given by the system, and then the initialization is displayed.
Local variables: Local variables must be initialized before they are used, otherwise they cannot be used.
There is a student class student, there are some attributes, in the test class to create a student class object, through the process?
Student s=new Student ();
1, through the test class inside main main program to load, load Student class (Student.class);
2, Student s first in the stack memory to open up the stack memory space, create a student object, the heap memory needs to request heap memory space;
3, through the non-parametric construction method to the Student class object default initialization;
4, by assigning values to the member variables, the object is displayed to initialize;
5, the display initialization is complete, after the whole object is created, the object is allocated the memory space address value;
6, find the corresponding object by the address value.
Object-oriented common face questions in Java