Member variables: defined in a class and owned by a class; can be modified by public, private, protect ed, static; stored in the heap; new objects must be used before being modified by static; all methods of the current class can be invoked; if the class has subclasses, subclasses can also be invoked; and no assignment can be made. If no primitive data type is assigned to return the corresponding value, the non-primitive data type returns null.
Definition:
public class Test{ public int num; }
Initialization method:
1. If only one member variable is defined without assignment, the compiler will automatically add the corresponding default values. Such as:
public class Test{ //Basic data types private boolean flag; //Default value: false private int _int; //Default value: 0 private byte _byte; //Default value: 0 private char _char; //Default value: '0000', but when printed in Eclipse, it will be the blank character "box", because the code of the blank character is 0x20, and the control character below 0x20 is invisible. private short _short; //Default value: 0 private float _float; //Default value: 0.0 private long _long; //Default value: 0 private double _double; //Default value: 0.0 //Reference type defaults to null private String string; }
2. Direct Assignment
public class Test { private int num = 1; private String name = "xiaomin"; public Person person = new Person(); }
3. Initialization by Method
public class Test{ private int i = f(); private int j = g(i); private int f() { return 10; } private int g(int i){ return i*2 } }
However, the compiler executes in sequence when initializing objects, so it cannot initialize in the following way:
public class Test{ private int j = g(i); private int i = f(); private int f() { return 10; } private int g(int i){ return i*2 } }
4. Initialization through constructors
public class Test{ int num ; public Test(){ num = 10; } }
Before entering the constructor, the compiler initializes num to 0 by default, and then assigns a value of 10 after entering the constructor.
Initialization order:
When compiling a class, member variables are first initialized, and the initialization order of member variables is determined by the order in which member variables are defined.
package com.extendstest; public class OrderOfInitialization { public static void main(String[] args) { Card card = new Card(); card.f(); } } class Tag { Tag(int num) { System.out.println("Tag" + num); } } class Card { Tag tag1 = new Tag(1); //Define tag1 variable Card() { System.out.println("Card()"); tag3 = new Tag(33); //tag3 is reinitialized } Tag tag2 = new Tag(2); //Define tag2 variables void f() { System.out.println("f()"); } Tag tag3 = new Tag(3); //Define tag3 variables }
In main method, a card object is created. When compiler compiles Card class, member variables tag1, tag2, tag3 are initialized before constructor Card(). Therefore, the output of the above code is as follows:
Tag1 Tag2 Tag3 Card() Tag33 f()
Initialization order of static variables: static member variables take precedence over non-static member variables.
public class StaticInitialization { public static void main(String[] args) { System.out.println("Creating new Cupboard() in main"); new Cupboard(); System.out.println("Creating new Cupboard() in main"); new Cupboard(); t2.f2(1); t3.f3(1); } static Table t2 = new Table(); static Cupboard t3 = new Cupboard(); } class Bowl{ Bowl(int i){ System.out.println("Bowl"+i); } void f(int i){ System.out.println("f"+i); } } class Table{ static Bowl b1 = new Bowl(1); Table(){ System.out.println("Table()"); b2.f(1); } void f2(int i){ System.out.println("f2("+i+")"); } static Bowl b2 = new Bowl(2); } class Cupboard{ Bowl b3 = new Bowl(3); static Bowl b4 = new Bowl(4); Cupboard(){ System.out.println("Cupboard()"); b4.f(2); } void f3(int i){ System.out.println("f3("+i+")"); } static Bowl b5 = new Bowl(5); }
Because static initialization only occurs once when the Class object is first created, the above code executes as follows:
Bowl1 Bowl2 Table() f1 Bowl4 Bowl5 Bowl3 Cupboard() f2 Creating new Cupboard() in main Bowl3 Cupboard() f2 Creating new Cupboard() in main Bowl3 Cupboard() f2 f2(1) f3(1)
There are also the following static block initialization methods:
class Cup{ Cup(int i){ System.out.println("Cup("+i+")"); } void f(int i){ System.out.println("f("+i+")"); } } class Cups{ static Cup c1; static Cup c2; static { //Static block initialization c1 = new Cup(1); c2 = new Cup(2); } Cups(){ System.out.println("Cups()"); } } public class StaticInitialization { public static void main(String[] args) { System.out.println("Inside main()"); Cups.c1.f(99); } static Cups x = new Cups(); static Cups y = new Cups(); }
Implementation results:
Cup(1) Cup(2) Cups() Cups() Inside main() f(99)
Another, non-static instance initialization, without the static keyword,:
package com.extendstest; class Mug{ Mug(int i){ System.out.println("Mug("+i+")"); } void f(int i ){ System.out.println("f("+i+")"); } } public class Mugs { Mug c1; Mug c2; { //Non-static initialization, which is called many times when an object is created c1 = new Mug(1); c2 = new Mug(2); System.out.println("c1&&c2inin...."); } Mugs(){ System.out.println("Mugs()"); } public static void main(String[] args) { System.out.println("inin..main"); Mugs x = new Mugs(); Mugs y = new Mugs(); } }
Implementation results:
inin..main Mug(1) Mug(2) c1&&c2inin.... Mugs() Mug(1) Mug(2) c1&&c2inin.... Mugs()