The difference between a class variable and an instance variable

Source: Internet
Author: User
Tags instance method
the difference between a class variable and an instance variable

Each time you create an object for a class, the system creates a copy of each instance variable for the class.
We can access the instance variable from the object. A class variable or a static variable is not the same as an instance variable, regardless of how many objects are created for a class, the system allocates only one storage space for each class variable. The memory that the system allocates for the class variable is Mark-to-win when the main method is executed, at the very beginning of the program (see the example of Staticdemo below). All objects share a class variable.
Class variables can be accessed either through an object or through the class itself. Static fields a field define as static, means there is only one such field shared by All Objects Instance fields A field D Efine without static, means every object has its own copy of fields by the way: the constants of the class are usually defined in the following form.
Static final double pi=3.14159 (accessible by either class or instance);
A static method (which is preceded by a static) and an instance method (not previously labeled static) call a static method or a class method by using the class name as a prefix or by using a specific object name, usually using the class name. A method that is not static is a method that belongs to an object. The static method, which belongs to the entire class, is not owned by any object alone; Because the static method belongs to the entire class, it cannot manipulate and process member variables belonging to an object, but only member variables that belong to the entire class, and that is , the static method can only handle static fields or static methods.
Instance methods can access instance fields, static fields, or static methods, and remember all rows.
The methods declared as static have the following limitations: 1. They can only invoke other static methods.
2. They can only access static data. 
3. They cannot refer to this or super in any way (the keyword super is related to inheritance, as described in the next chapter). static method Have No-Reference All instance methods Have a hidden parameter-this so, Static method can ' t access ins Tance methods and fields; It can only invoke the other static Class members. 
It can access class members only.
Instance can use static method. Instance Methods:if A is declared without the static modifier keyword, which is known as a Instance method .
Instance methods are associated with objects–not classes. 

It can access either instance or class members. 
Class Staticexa {static int a = 4; static int b = 9; static void Call () {System.out.println ("a =" + a);//static method can access static properties} public class Test {static int c = \ public static void Main (String args[]) {/* When you first run to this step, debug observes that the STATICEXA.A value equals 42,t The est.c value equals 43, which means that the system assigns values to all class variables at the beginning of our program. If the object reference is null, see Photoshop Example/System.out.println ("Start observing staticexa.a and test.c");

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.