The difference between object-oriented _ member variables and local variables

Source: Internet
Author: User

Scope of Action:

A local variable acts in a function or statement (defined in a function or statement (such as a for statement) and must be initialized)

Member variables are scoped to the entire class (defined in the class, with default initialization values, you can define display initialization values)

In-memory location:

The member variable is in heap memory. Because the object exists, it exists in memory.

Local variables are in the stack memory.

If the member variable does not have a defined display initialization, then the system is initialized by default (the variables in the heap memory will have default initialization values)
BYTE 0
Short 0
int 0
Long 0L
Double 0.0D
Float 0.0f
Boolean false
Reference data type default initialization value is NULL

1  Public classVariabledemo {2      Public Static voidMain (string[] args) {3Car c=NewCar ();4C.color= "Blue";5 C.run ();6     }7 }8 classcar{9     //member variables, defined in the class, acting on the entire class, have default initialization values. (can define display initialization)TenString color= "Red"; One     intNum=4; A     //member functions -     voidrun () { -System.out.println (color+ "...." +num); the     } -}
 1  class   car{ 2  String color= "Red" ; 3   4  void   run () { 6  int  num = 9; //  in the member is accessed. 7  System.out.println (color+ ".... +num");  8   9  }

One class accesses a non-static member in another class, the class object must be created

1 classperson{2     intAge ;3      Public voidShurt () {4         intAge = 89;//Local variables must be initialized5System.out.println (age);//because there is an age variable in the function, access is accessed at the time of the Shurt function, and the external member variable is hidden.6     }7 }8 //method of Java recycling mechanism: Finalize ();9 //the method of waking up the Java recycle mechanism is System.GC (); (Calling the method Java virtual machine does not necessarily perform garbage collection immediately)
1  Public classVariableTest2 {2      Public Static voidMain (string[] args) {3         intx = 5;4 Change (x);5 System.out.println (x);6     }7      Public Static intChangeintx) {8         returnx = 4;9     }Ten } One //The result is still 5, because X is a local variable, and when the change method is executed, the X in the Change method is released, which is not the same as the X in the main method.

The difference between object-oriented _ member variables and local variables

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.