main method
* Description main () method * main method of the main entrance to the program (main program start a main execution starts) * * * main method may be an ordinary static method
Block
A member of the class of variable code block is divided into static and non-static code block code block
static { // static block of code } // non-static block { }
* Class of four members: a member of a code block (or initialization code block) class: attribute method, constructor, code block * <P> * <P> *. 1 action code block: is used to initialize a class or object * 2 . If there is a block of code modification, you can only use static * 3. classification: static code block, non-static code block
eg:
public class codeblock { public static void main (String [] args) { String desc = BlockPerson.desc; // when I is static code block BlockPerson blockPersonOnew = new new BlockPerson (); // I non-static block } } class BlockPerson { name String; int Age; static String desc = "I am a person" ; // constructor public BlockPerson () { } public BlockPerson (String name, int Age) { the this .name =name; the this .age = Age; } // block static { // static block of code with class loading and loading System.out.println ( "static code block I" ); } { // non-static block System.out.println ( "non-static code block I" ); } @Override public String toString () { return "the Person [name:" + name + "Age:" + Age + "]" ; } public static void info () { System.out.println ( "I'm a man" ); } }
Non-static block of code can call the static methods and properties
{ // non-static code block System.out.println ( "I am a non-static code block" ); Age = 1 ; desc = "I'm a love of learning people"; // non-static block of code can call the static structure info ( ); }
The difference between static and non-static block code block
4 . Static code: *> interior can output statement *> with loading the class is performed, and is performed only once *> initialize class attributes *> If a static class defines a plurality of code blocks, in accordance with the declaration has sequential execution *> perform a static code block priority to non-static block of code (as with the static block of code is executed to load the class, non-static is performed after a subject in need instantiated) *> the static block of code can calling the static properties and non-static methods can not call * 5 . non-static block *> interior can output statement *> with the creation of the object is performed *> each object is to create a block of code to perform a non-static, long type without reloading will not be re-executed *> effect: you can create an object is an object attribute assignment can call static and non-static properties and methods *>
Object assignment method:
The method of the object assignment: * position properties can be assigned to: * 1 . Default initialization * 2 . Display initialize / 5 . In the code block assignment (assignment who who should first written) * 3 constructor initializes. * 4. There after the object can be "Object properties." or "object method." assignment sequence is: 1> 2/5> 3> 4
eg:
public class OrderTest { public static void main(String[] args) { Rder rder = new Rder(); System.out.println(rder.age); //4 } } class Rder{ int age = 3; // 显示赋值 { age = 4; } }
Object assignment
Interview questions:
Here, the class file called Something OtherThing.java class Something { public static void main (String [] something_to_do) { System.out.println ( "the Do something ..." );} } above normal program is compiled, run ?
result:
Compile and run, but the statement can not be output because there is no master class master class main method does not perform
Exercises:
The results of the following to say:
class the Person { public static int Total; static { Total = 100 ; System.out.println ( "static in Block!" );} } Public class PersonTest { public static void main (String [] args) { System.out.println ( "Total =" + Person.total); System.out.println ( "Total =" + Person.total);
} }
result:
Since a static block is performed with loading the class as class loading precedence to create objects so that the output statement executed static code block, then the output of the lower face of the property called in static Block Total = 100 Total = 100
Here is the output topics:
// summary: the Father and the Son, static first public class LeftTest { public static void main (String [] args) { new new Left (); } } class Root { static { System.out.println ( "I am the Root static code block " ); } { System.out.println ( " I Root block non-static " ); } public Root () { System.out.println ( " I Root no arguments constructor " ); } } class Mid the extends Root { static { System.out.println ( "I Mid static code block" ); } { System.out.println ( "I Mid block non-static" ); } public Mid () { System.out.println ( " I Mid no arguments constructor " ); } public Mid (String MSG) { the this (); System.out.println ( " Mid constructor arguments, parameters are: MSG: "+ MSG); } } class Left the extends Mid { static { System.out.println ( "my Left the static code block" ); } { the System.out.println("I left the block non-static" ); } public Left () { System.out.println ( "I left, no arguments constructor" ); } public Left (String MSG) { Super (); the System. Out.println ( "my Left is the constructor parameter, the parameter is MSG:" + MSG); } }
result:
* I am a static code block of Root * I'm Mid static block of code * I is a static code block the Left * I'm a non-static code block of Root * I was Root's no-argument constructor * I'm Mid non-static code block * I am the Mid no arguments constructor * I am a non-static block of code left the * I was left in no-argument constructor
The following output:
class FatherTest { static { System.out.println("111111"); } { System.out.println(22222); } public FatherTest() { System.out.println(33333); } } public class SonTest extends FatherTest { static { System.out.println("44444444"); } { System.out.println("55555555"); } public static void main(String[] args) { System.out.println("777777777"); System.out.println("**********"); new SonTest(); System.out.println("+++++++++++"); new SonTest(); System.out.println("========"); new FatherTest(); } }
result:
111111 44444444 777777777 ********** 22222 33333 55555555 +++++++++++ 22222 33333 55555555 ======== 22222 33333