Interview questions: Java instance variables, local variables, class variables

Source: Internet
Author: User
Tags instance method

First, instance variables

Also called an object variable, a class member variable, the storage space is allocated when the object is generated from the class, and the instance variables between the objects do not interfere with each other, and the instance variables can be accessed through the object's reference. However, in Java Multi-threading, instance variables are multiple threads that share resources, and you should be aware of the problems that may occur when you synchronize access.

[Java]View PlainCopy
  1. <span style="FONT-SIZE:14PX;" >public class Demo {
  2. //The following are instance variables (member variables, object variables)
  3. private String namestring;
  4. public int age;
  5. protected int priority;
  6. //Instance method
  7. Public String getnamestring () {
  8. return this.namestring;
  9. }
  10. }</span>

Second, class variables

Also known as a static variable, is a special instance variable, decorated with the static keyword, a class of statically variable, all the objects generated by this class share this class variable, the class load allocates storage space. When an object modifies a variable, the value of the variable in the object changes.

[Java]View PlainCopy
    1. <span style="FONT-SIZE:14PX;" >public class Demo {
    2. //class variable (static variable)
    3. public static int a = 0;
    4. //Instance variable
    5. private String namestring;
    6. }</span>

Third, local variables

A parameter in a method or a local block that declares a defined variable or method is called a local variable, and they exist only in the block where they were created ({}) and cannot perform any operations outside the block, such as reading or assigning values. In Java Multi-threading, each thread replicates a local variable to prevent some synchronization problems from occurring.

[Java]View PlainCopy
  1. <span style="FONT-SIZE:14PX;" > //class variables (static variables)
  2. public static int a = 0;
  3. //Instance variable
  4. private String namestring;
  5. public Void Test () {
  6. //Local variables
  7. int temp = 1;
  8. SYSTEM.OUT.PRINTLN (temp);
  9. }</span>


Iv. the difference between them

The difference between a member variable and a local variable

Member variables:

The ① member variable is defined in the class and can be accessed throughout the class.

The ② member variable is created as the object is built, disappears as the object disappears, and exists in the heap memory where the object resides.

The ③ member variable has a default initialization value.

Local variables:

① Local variables are defined only within the local scope, such as within a function, within a statement, and only in the area to which they belong.

② local variables exist in the stack memory, the scope of the action ends, and the variable space is automatically freed.

③ local variable has no default initialization value

The principles to be followed when using variables are: proximity principle

First, in the local scope to find, have to use; then find it in the member location

The difference between a member variable and a static variable

1, two variables have different life cycles

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

Static variables exist as the class loads, disappearing as the class disappears.

2. Different calling methods

Member variables can only be called by an object.

A static variable can be called by an object and can also be called by a class name.

3. Different aliases

Member variables are also known as instance variables.

A static variable is also known as a class variable.

4. Different Data storage locations

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

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.

Interview questions: Java instance variables, local variables, class variables

Related Article

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.