The differences between class variables, instance variables, class methods, and instance methods in Java

Source: Internet
Author: User
Tags instance method

class variable : shape as static int A; As the name implies, a class variable can be understood as a variable of a class, and a class variable is allocated a memory space when the class is loaded, unlike an instance variable (int A; ), an instance variable allocates memory when the class creates an object. And, each time an object is created, the instance variable is allocated the appropriate memory space, and the class variable does not belong to any one object, it is shared with all objects, and the class variables of all objects occupy the same memory space. The class name can be used during the invocation of a class variable . Class variables can also be called by the object name. Class Variable . The class variable is the same as the static variable that is contacted in C + + and has memory function.

class method: static void Show (); Like class variables, class methods are also part of the class, and when the class is loaded, it assigns the entry address to the class method, and the instance method is assigned the entry address only after the object is created. Similarly, the class method can be called by all pairs, and can be called with the class name . class method or object name. class Method . Instance methods can only be called with object names. It is important to note, however, that class methods cannot invoke instance variables, because class methods are allocated at the time the class is loaded, and at this point the object may not be created to allocate memory space for the instance variable, that is, the instance variable may not exist, so you cannot invoke the instance variable with a class method. An instance method can invoke all instance variables and class variables.

Both instance variables and instance methods release memory space when the object dies, and class variables and class methods release all memory space until the program runs out of date.

The differences between class variables, instance variables, class methods, and instance methods in Java

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.