Java中static靜態變量的初始化完整解析。本站提示廣大學習愛好者:(Java中static靜態變量的初始化完整解析)文章只能為提供參考,不一定能成為您想要的結果。以下是Java中static靜態變量的初始化完整解析正文
靜態變量初始化次序
1.簡略規矩
起首先看一段最廣泛的JAVA代碼:
public class Test
{
public static Test1 t = new Test1();
public static int a = 0;
public static int b;
public static void main(String[] arg)
{
System.out.println(Test.a);
System.out.println(Test.b);
}
}
class Test1
{
public Test1()
{
Test.a++;
Test.b++;
}
}
這裡先猜下掌握台輸入成果是甚麼?
OK, 也許你曾經猜到上面了卻果了,那末你照樣熟習Java的。
0 1
假如你不明確是為何會輸入下面的成果,那末我來告知你。
Java靜態變量初始化遵守以下規矩:
看了這個就會明確,本來Test.a的值變更了三次。
聲明時設置為0>>Test1::Test1裡設置為1>>Test.a初始化為0
2.龐雜規矩
明確了這個,請再看上面的代碼。
public class A
{
public static int b = B.a;
public static A plus =new A("A");
public static final int finalInt = (int)(Math.random()*100);
public static B p = new B("A");
public static final String finalStr = "finalStr";
public static final Integer finalInteger = new Integer(10);
public static int a = 1;
public static B c = null;
public A(String from)
{
System.out.println("----------- begin A::A ----------------");
System.out.println("A::A, from="+from);
System.out.println("A::A, A.b="+A.b);
System.out.println("A::A, A.finalInt="+A.finalInt);
System.out.println("A::A, B.a="+B.a);
System.out.println("A::A, B.plus="+B.plus);
System.out.println("----------- end A::A ----------------");
}
public static void main(String[] arg)
{
System.out.println("main, A.b="+A.b);
System.out.println("main, B.t="+B.t);
System.out.println("main, C.a="+C.a);
}
}
class B
{
public static int t = A.a;
public static A plus = new A("B");
public static int a = 1;
public B(String from)
{
System.out.println("----------- begin B::B ----------------");
System.out.println("B::B, from="+from);
System.out.println("B::B, B.a="+B.a);
System.out.println("B::B, A.a="+A.a);
System.out.println("B::B, A.p="+A.p);
System.out.println("B::B, A.plus="+A.plus);
System.out.println("B::B, A.finalInt="+A.finalInt);
System.out.println("B::B, A.finalInteger="+A.finalInteger);
System.out.println("B::B, A.finalStr="+A.finalStr);
System.out.println("----------- end B::B ----------------");
}
}
class C
{
public static final A a = new A("C");
}
這個你還能猜到輸入成果嗎? 我是在一邊測試一邊寫的,所以我沒猜出來.哈哈
掌握台輸入成果為:
----------- begin A::A ---------------- A::A, from=B A::A, A.b=0 A::A, A.finalInt=0 A::A, B.a=0 A::A, B.plus=null ----------- end A::A ---------------- ----------- begin A::A ---------------- A::A, from=A A::A, A.b=1 A::A, A.finalInt=0 A::A, B.a=1 A::A, B.plus=A@a90653 ----------- end A::A ---------------- ----------- begin B::B ---------------- B::B, from=A B::B, B.a=1 B::B, A.a=0 B::B, A.p=null B::B, A.plus=A@1fb8ee3 B::B, A.finalInt=61 B::B, A.finalInteger=null B::B, A.finalStr=finalStr ----------- end B::B ---------------- main, A.b=1 main, B.t=0 ----------- begin A::A ---------------- A::A, from=C A::A, A.b=1 A::A, A.finalInt=61 A::A, B.a=1 A::A, B.plus=A@a90653 ----------- end A::A ---------------- main, C.a=A@61de33
這個成果你沒猜到吧,哈哈.
要一句一句的講授法式履行成果,照樣要很到的篇幅的.這裡就直接寫出Java靜態變量初始化遵守的規矩了。
第一段的規矩仍然有用,只是不健全。
static數據的初始化
加上static限制的字段,是所謂的類字段,也就是說這個字段的具有者不是對象而是類。不管創立若干對象,static數據都只要一份。
類內老是先初始化static字段,再初始化普通字段。接著初始化結構器。然則假如不創立這個類的對象,那這個對象是不會停止初始化的,而且只履行一次。
以下面的代碼,在StaticInitialization類中,先初始化static Table table = new Table();,然後才去初始化Table對象,否則是不會被初始化的。
class Bowl {
Bowl(int marker) {
print("Bowl(" + marker + ")");
}
void f1(int marker) {
print("f1(" + marker + ")");
}
}
class Table {
static Bowl bowl1 = new Bowl(1);
Table() {
print("Table()");
bowl2.f1(1);
}
void f2(int marker) {
print("f2(" + marker + ")");
}
static Bowl bowl2 = new Bowl(2);
}
class Cupboard {
Bowl bowl3 = new Bowl(3);
static Bowl bowl4 = new Bowl(4);
Cupboard() {
print("Cupboard()");
bowl4.f1(2);
}
void f3(int marker) {
print("f3(" + marker + ")");
}
static Bowl bowl5 = new Bowl(5);
}
public class StaticInitialization {
public static void main(String[] args) {
print("Creating new Cupboard() in main");
new Cupboard();
print("Creating new Cupboard() in main");
new Cupboard();
table.f2(1);
cupboard.f3(1);
}
static Table table = new Table();
static Cupboard cupboard = new Cupboard();
}
輸入:
Bowl(1) Bowl(2) Table() f1(1) Bowl(4) Bowl(5) Bowl(3) Cupboard() f1(2) Creating new Cupboard() in main Bowl(3) Cupboard() f1(2) Creating new Cupboard() in main Bowl(3) Cupboard() f1(2) f2(1) f3(1)
顯示的靜態初始化(也就是靜態塊)
把多個初始化語句包在一個static花括號裡,叫做靜態塊,其實就是把多個static合在一路寫了,實質是一樣的。只要初次創立對象或許初次拜訪類的字段時才會履行,並且僅僅一次。
class Cup {
Cup(int marker) {
print("Cup(" + marker + ")");
}
void f(int marker) {
print("f(" + marker + ")");
}
}
class Cups {
static Cup cup1;
static Cup cup2;
static {
cup1 = new Cup(1);
cup2 = new Cup(2);
}
Cups() {
print("Cups()");
}
}
public class ExplicitStatic {
public static void main(String[] args) {
print("Inside main()");
Cups.cup1.f(99); // (1)
}
// static Cups cups1 = new Cups(); // (2)
// static Cups cups2 = new Cups(); // (2)
}
輸入:
Inside main() Cup(1) Cup(2) f(99)
非靜態實例初始化
這個沒甚麼好講的,就是通俗初始化,按次序履行,可以屢次履行。
class Mug {
Mug(int marker) {
print("Mug(" + marker + ")");
}
void f(int marker) {
print("f(" + marker + ")");
}
}
public class Mugs {
Mug mug1;
Mug mug2;
{
mug1 = new Mug(1);
mug2 = new Mug(2);
print("mug1 & mug2 initialized");
}
Mugs() {
print("Mugs()");
}
Mugs(int i) {
print("Mugs(int)");
}
public static void main(String[] args) {
print("Inside main()");
new Mugs();
print("new Mugs() completed");
new Mugs(1);
print("new Mugs(1) completed");
}
}
Inside main() Mug(1) Mug(2) mug1 & mug2 initialized Mugs() new Mugs() completed Mug(1) Mug(2) mug1 & mug2 initialized Mugs(int) new Mugs(1) completed