Analysis of the difference of Java variables

Source: Internet
Author: User
Tags int size

  for Java variables to see every day, but sometimes people ask them the difference is sometimes not complete, so I found some information to organize ...

Class variables are also called static variables, that is, a static variable is added to the variable; the instance variable is also called an object variable, that is, a variable without static; the difference is that a class variable (a static variable) is common to all objects, one of which changes the value of the object, and the other objects get the result of the change, and class variables can be called directly through the class name, such as: A.xings, while the instance variable belongs to the object private, an object will change its value, does not affect other objects, instance variables as the name implies that it belongs to the instance, so only through the instance to invoke such as: a A=new a (); A.name An inappropriate example: A class variable is like a person's surname, an instance variable is the name of a person, and if his father changes his surname, all its siblings have to be changed, but their names are all the same. Example: The code is as follows: public class a{  static  int xings = 0;//class variable    private int name = 0;//instance variable    S Tring 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 (); &N Bsp      a son2= new A ();        son1.xings = 3;  //equivalent to   a.xings = 3;        son1.name = 4;        System.out.println (son2.xings); The result is 3       //class variables are for all objects, so son1 change xings,sOn2 's a also changes        System.out.println (son2.name); Results for 0       //instances only change their own, so the name of the Son1 object changes, does not affect the object Son2 name variable  }} The   class variable is also called static member variable, It does not need to create an object to already exist in memory, and when creating an instance object, in memory, a memory space is created for each non-static member variable of each instance object to store all the non-static member variable values of the object. Even if two different instance objects belong to the same class class, the space in memory occupied by their Non-static member variables of the same name is different, and the static member variable is the same as the class variable. All instance objects share a class variable, and only one space in memory is placed in the class variable value. Therefore, if an object changes the value of the class variable, the other object takes the class variable value to be changed.  
Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.