Differences between java Variables

Source: Internet
Author: User

Differences between java Variables

Java variables are seen every day, but sometimes the difference between them is not all done, so I sorted out some materials...

A class variable is also called a static variable, that is, a static variable is added before the variable;

Instance variables are also called object variables, that is, variables without static;

The difference is:

A class variable (static variable) is shared by all objects. One of the objects changes its value. Other objects get the changed result, in addition, class variables can be called directly through class names, for example,. xingS;

Instance variables are private objects. An object changes its value without affecting other objects. instance variables, as the name suggests, belong to instances. Therefore, they can only be called through instances, for example: A a = new A ();. name;

For example, a class variable is like a person's last name. An instance variable is equivalent to a person's name. If his father changes his last name, all of its siblings have to change their surnames, but their names are different.

Example:

The Code is as follows:

Public class {

Static int xingS = 0; // class variable

Private int name = 0; // instance variable

String id; // instance variable

Private String colorType; // instance variable

Private int size; // instance variable

Private static String depart; // class variable f

Inal String name = "zwm"; // constant

}

Public class B {

Public void main (String [] args ){

A son1 = new ();

A son2 = new ();

Son1.xingS = 3; // equivalent

A. xingS = 3;

Son1.name = 4;

System. out. println (son2.xingS); // The result is 3.

// Class variables are for all objects, so son1 changes xingS and son2's a also changes

System. out. println (son2.name); // The result is 0.

// The instance only changes its own, so the name of the son1 object does not affect the name variable of the object son2

}

}

 

A class variable is also called a static member variable. It can already exist in the memory without the need to create an object. When creating an instance object,

The memory will open up a memory space for each non-static member variable of each instance object to store all the non-static member variable values of this object.

Even if two different instance objects belong to the same class, their non-static member variables with the same name occupy different spaces in the memory,

Static member variables, that is, class variables, are the same. All instance objects share a class variable. Only one space in the memory is included in the class variable value.

Therefore, if an object changes the value of a class variable, and another object obtains the value of a class variable, it is changed.

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.