Understand Java and Python class variables and class member variables _java

Source: Internet
Author: User

The scariest thing is not to make mistakes, but to never find them, until now I know I have a problem with the understanding of class variables.
Maybe it's probably because of the less common class variables that haven't been identified . I recently saw C + + to know what the class variable in the end is what ?
I've always felt that the only difference between a class variable and a member variable is that the class variable can be accessed directly by the class name and is static. A member variable needs to instantiate a class and then access it through an instance.
Never thought of ignoring it. Class variables are only one in a class, all instances are the same, and in one instance the modification affects class variables in other instances ... (although there is no usual bug caused by this, it still needs to be filled with cognitive vulnerabilities).
Here are 2 examples of Java and Python written :

public class oo{public
  static String s;
  Public String m;

  static{
    s = "Ever";
  }
  public static void Main (string[] args) {
    oo O1 = new OO ();
    oo O2 = new oo ();

    O1.M = "Once";

    The class variable value/address is the same in different instances
    System.out.println (O1.S);
    System.out.println (O2.S);
    System.out.println (O1.s.hashcode ());
    System.out.println (O2.s.hashcode ());

    O1.S = "123";
    System.out.println (O2.S);//changing class variable affects other instance

    System.out.println (O1.m.hashcode ());
    System.out.println (O2.m.hashcode ());//nullpointerexception
    //member variable has a different address
  }

}


#!/bin/python

class B:
  def whoami (self):
    print ("__class__:%s,self.__class__:%s"% (__class__,self.__ CLASS__)

class C (B):
  count = 0

  def __init__ (self):
    super (C,self). __init__ ()
    self.num = 0

  def add (self):
    __class__.count = 1
    self.num + + 1

  def print (self):
    print ("count_id:%s,num_id:%s" % (ID (__class__.count), ID (self.num)))
    print ("count:%d,num:%d"% (__class__.count,self.num))

I1 = C ()
i2 = C ()
I1.whoami ()
I2.whoami ()
#i1的成员变量增加了1次, the membership variable of i2 was increased 2 times, and the class variable was increased by 3 times
I1.add ()
I2.add ()
i2.add ()
i1.print ()
i2.print ()

The above is the entire content of this article, tomorrow holiday is over, I hope everyone actively into the work, continue to focus on small series for everyone to share the article.

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.