1. What is Static Yes? What is the use?
Static main role is to create independent of the specific object or variable domain method.
Each create an object on the heap will open up memory, save members of the (property), but the method does not exist, the method is shared, each object is not necessary to waste memory to store method. There is a place called the method area, special deposit method. Methods district there are static fields, keep static variables or static methods.
Call ordinary variables and methods: The object calls
Call static variables and methods: through an object or object calls
public class MyTest8 { public static void main (String [] args) { System.out.println (Plant.name); // by calling class Plant P1 = new new Plant (); System.out.println (p1.name); // call through an object p1.name = "plant"; // similar approach is common, after changed is changed, the new object is also the value plant P2 = new new plant (); System.out.println (P2. name); // call through the object } } class plant { static String name = "static plant" ; public static void say () { System.out.println ( "Plant" ); } } / ** Output static plant static plant Plant * /
When the class is loaded, the static code will be loaded and loaded only once, in the method area, together with the objects of this class. save time and energy.
2. Load class what's the use?
Any program must first be loaded into memory and CPU to communicate, but the JVM ClassLoader (class loader) is responsible for in advance, class file is loaded into memory to go.
3. When classes are loaded? (Ordinary people understand the macro)
- When the object is instantiated, such as Chinese c1 = new Chinese (); Chinese case loaded classes
- Static variable or static method call by class name when
- If an instance subclasses of objects, will first load the parent class
By code validation:
public class MyTest8 { public static void main (String [] args) { Plant.say (); // class name calling static methods Plant P1 = new new Plant (); // Create Object Plant P2 = new new Plant (); } } class plant { static String name = "static plant" ; public static void say () { System.out.println ( "plant" ); } static { System.out.println ( "static plant-based code is executed to load a " ); } { System.out.println ( "normal code is loaded the plant-based" ); } }
Output:
Plant-based static code is executed loaded with
plant-based
common plant-based codes are loaded with
ordinary plant-based code that is loaded
It can be seen, Plant.say (); method call loads the class, static code have also been carried out and executed only once, create an object of class time to reload, but does not perform static code, but a non-static code .
Then observe static and non-static relations through inheritance
public class MyTest8 { public static void main (String [] args) { Flower.say (); // call static methods through the class name Flower f1 = new new Flower (); // create an object Flower Flower F2 = new new Flower () ; } } class plant { static String name = "static plant" ; public static void say () { System.out.println ( "plant" ); } static { System.out.println ( "static code is plant-based performed loaded " ); } { System.out.println ( "normal code is loaded the plant-based" ); } } class Flower the extends Plant { static { System.out.println ( "Flower was loaded" ); } static String name = "flower " ; public static void say () { System.out.println ( " flowers " ); } { System.out.println ( " normal code is loaded flower " ); } }
Output:
Plant-based static code is executed to load the
flowers are loaded with
flowers
ordinary plant-based code that is loaded with
normal code flowers are loaded with
ordinary plant-based code that is loaded with
normal code is loaded with flowers
It can be seen: call the static thing, only performs static code, static code is executed only once. When creating an object will load, if the static had not been loaded will be loaded, do not over-loaded. When loaded subclass the parent class will first be loaded once.
In the code, sub-class static method call, first load the static code of the parent class, and then load the static code subclass, calls the method. Create an object when the first parent class is loaded, and then load the subclass.