1. Why is the following code not compile? Where is wrong?
The system provides a method of custom, the system no longer cause provide a default constructor. Resulting in code compiler can not pass.
What next 2 code output is?
package text1; public class InitializeBlockClass { { field = 200; } public int field = 100; public InitializeBlockClass(int value){ this.field = value; } public InitializeBlockClass(){ } public static void main(String[] args) { // TODO Auto-generated method stub InitializeBlockClass obj = new InitializeBlockClass(); System.out.println(obj.field); obj = new InitializeBlockClass(300); System.out.println(obj.field); } }
Java field initialized law:
1. When performing the specified class member define default or initialization block type, a top surface of which which is executed first.
2. Perform class constructor.
3. Run TestStaticInitializeBlock.java example, look at the output, concludes that "static initialization block execution order."
Root class { static { System.out.println ( "Root static initialization block"); } { System.out.println ( "normal initialization block the Root"); } public Root () { System.out.println ( " Root no argument constructor "); } } class Mid the extends Root { static { System.out.println (" Mid static initialization block "); } { System.out.println (" Mid general initialization block ") ; } public Mid () { System.out.println ( "Mid no argument constructor"); } public Mid (String MSG) { // call the same class constructor overloads by the this the this (); the System .out.println ( "Mid constructor parameters with which parameter values:"+ msg);+ msg); } } Class Leaf the extends Mid { static { System.out.println ( "Leaf static initialization block"); } { System.out.println ( "Leaf normal initialization block"); } public Leaf () { // the super call the parent class has a constructor parameter string of super ( "Java initialization sequence demo"); System.out.println ( "Leaf performed constructor"); } } public class TestStaticInitializeBlock { public static void main (string [ ] args) { new new Leaf (); } }
Static initialization block execution order:
1. static initialization block is executed only once.
2. Create a sub-type of the object, it will lead to the implementation of static initialization block of the parent type.
4. Static method only allows access to static data, then, how in a static method access instance members of the class (ie no additional field or method static keyword)?
{class Jingtai public public int X = 200 is; public static int Y = 300; public static void Method () { System.out.println ( "instance variables x =" + new jingtai () x.); // the class static method access instance variables in the class System.out.println ( "static variable = Y" + Y); } public static void main (String [] args) { Jingtai .method (); Jingtai Jingtai EX = new new (); System.out.println ( "X =" + ex.x); } }
Examples of the access member in a static method needs for the implementation of the constructor of the class dependent class instance object before.
Class static methods or properties, the essence is not a member of the class, in the java virtual machine installed at the time of the class, these things have a static object, it is only in this class, "the stranger", without going through the class constructor (the constructor) implementation example of the class; not static property or method, the loading of the class is not present, is intended for dependent class after performing a constructor for the class instance of the object exists.