头图

在类中代码的执行顺序:

1 、先执行静态代码块。

static {
    System.out.println("static code execute");
}

public static void main(String[] args) {
    System.out.println("main() execute");
}

执行结果 :

static code execute
main() execute

2 、类中定义静态变量对象 ,会先创建对象。

 static {
    System.out.println("static code execute");
}
static User user=new User();

public static void main(String[] args) {
    System.out.println("main() execute");
}

执行结果 :

static code execute
User
main() execute

静态代码一定先于main方法执行 ,静态代码块和静态成员变量的执行顺序由位置决定 ,谁放在前面就先执行谁。

3 、如果是非静态代码块和成员变量 ,不执行。

{
    System.out.println("static code execute");
}

User user = new User();

public static void main(String[] args) {
    System.out.println("main() execute");
}

执行结果 :

main() execute

只有在创建Test对象时 ,才会执行非静态代码块和非静态成员变量。

 {
    System.out.println("static code execute");
}

User user = new User();

public static void main(String[] args) {
    System.out.println("main() execute");
    Test test = new Test();
    Test test1 = new Test();
    Test test2 = new Test();
}

执行结果 :

main() execute
static code execute
User
static code execute
User
static code execute
User

创建多少个对象 ,就会执行多少次代码块 ,创建多少成员变量 ,非静态代码块和非静态成员变量的执行顺序由位置决定 ,谁放在前面就先执行谁。

4 、如果同时存在静态代码块和非静态代码块 ,以及非静态成员变量和静态成员变量 ,先执行静态的东西 ,并且只执行一次,再执行非静态的东西(创建对象),创建多少个对象就会执行多少次。

 {
    System.out.println("static code execute");
}

User user = new User();
static User user1 = new User();

static {
    System.out.println("Static code execute");
}

public static void main(String[] args) {
    System.out.println("main() execute");
    Test test = new Test();
    Test test1 = new Test();
    Test test2 = new Test();
}

执行结果 :

User
static code execute
main() execute
static code execute
User
static code execute
User
static code execute
User

5 、父子类

 public static void main(String[] args) {
    Child child = new Child();
    child.Sayhello();
    ((Parent)child).Sayhello();
}

Stub

public Stub(String str) {
    System.out.println(str + "object create");
}

父类

public class Parent {
static Stub parentStaticStub = new Stub("parent static object -");

static {
    System.out.println("parent static code execute");
}
{
    System.out.println("parent static code execute");
}
Stub parentstub = new Stub("parent object -");

Stub stub;
public Parent(){
    System.out.println("parent constructor execute");
    stub = new Stub("parent sonstructor create objiect-");
}

public void Sayhello(){
    System.out.println("hello from parent");
}
}

子类

public class Child extends Parent {
static Stub ChildStaticStub = new Stub("child static object -");

static {
    System.out.println("child static code execute");
}
{
    System.out.println("child static code execute");
}
Stub childstub = new Stub("child object -");

Stub stub;
public Child(){
    System.out.println("child constructor execute");
    stub = new Stub("child sonstructor create objiect-");
}

public void Sayhello(){
    System.out.println("hello from child");
}
}

执行结果 :

parent static object -object create
parent static code execute
child static object -----object create
child static code execute
parent static code execute
parent object -object create
parent constructor execute
parent sonstructor create objiect-object create
child static code execute
child object -object create
child constructor execute
child sonstructor create objiect-object create
hello from child
hello from child

分析 :

1 、首先会加载Parent ,则Parent中的静态代码块和静态成员方法先执行。

执行结果 :

parent static object -object create
parent static code execute

2 、加载Child ,则Child中的静态代码块和静态成员方法先执行。

执行结果 :

child static object -----object create
child static code execute

3 、类加载完成之后,创建对象,先创建Parent对象,创建对象前,先创建对象的资源。

执行结果 :

parent static code execute
parent object -object create

4 、执行Parent构造器 ,完成对象创建。

执行结果 :

parent constructor execute
parent sonstructor create objiect-object create

5 、创建对象前 ,先创建Child的资源。

执行结果 :

child static code execute
child object -object create

6 、执行Child构造器 ,完成创建。

执行结果 :

child constructor execute
child sonstructor create objiect-object create

7 、执行Say hello方法。

执行结果 :

hello from child
hello from child

尽管执行了强制类型转换,但是实际上对象本身还是内存中的子对象,所有hello都来自child。


Sonner
1 声望0 粉丝

正在努力的999