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");