static

作者: 王古 | 来源:发表于2019-08-03 10:48 被阅读0次
    public class staticTest {
        
        Person person = new Person("Test");
        static {
            System.out.println("tset static");
        }
        
        public staticTest() {
            System.out.println("test constructor");
        }
        
        public static void main(String[] args) {
            new Myclass();
        }
    }
    
    class Person{
        
        static {
            System.out.println("person static");
        }
        
        public Person(String str) {
            System.out.println("person " + str);
        }
    }
    
    class Myclass extends staticTest{
        Person person = new Person("Mycalss");
        static {
            System.out.println("myclass static");
        }
        
        public Myclass() {
            System.out.println("myclass contructor");
        }
    }
    
    

    输出结果:

    tset static
    myclass static
    person static
    person Test
    test constructor
    person Mycalss
    myclass contructor
    

    解释:

    类加载过程:加载,验证,准备,解析,初始化(类构造器<!cliint>)
    
    加载(父类-子类),初始化成员变量,初始化执行构造器
    

    相关文章

      网友评论

          本文标题:static

          本文链接:https://www.haomeiwen.com/subject/mcxjdctx.html