static inner class, instance inner class

Table of contents

static inner class

instance inner class


static inner class

How do we define a static inner class?

In an external class, the static class class name (it can be understood as a member, but it is basically a variable that is not a method but a class)

class OutClass1 {

    static class InnerClass {
        //这里定义一个static变量为后面讲实例内部类铺垫
        public static int data6 = 1;
    }

}

We created static inner class, so how can we get static inner class object?

Here we know that static variables do not depend on objects, so there is no need to new an Outclass1 object first (it will be used when talking about the inner class of the instance), you can directly new OutClass.InnerClass().

public class test1 {

    public static void main(String[] args) {

        OutClass1.InnerClass innerClass = new OutClass1.InnerClass();

    }

}

So since the static inner class is created first, how to access non-static member variables directly in the static inner class?

This problem is very simple, just new an external class!

class OutClass1 {

    public int data = 1;

    static class InnerClass {

        OutClass1 outClass1 = new OutClass1();

        public void test() {

            System.out.println(outClass1.data);

        }

    }

}

instance inner class

How do we define an instance inner class?

It is similar to the static inner class above, just remove the static

class OutClass1 {

    class InnerClass {

    }

}

How to create static variable in instance inner class?

When talking about static inner classes above, remember that I defined a static variable in the static inner class? Of course, it can be created like that in a static inner class, but how to create it in an instance inner class?

We can understand it this way: the static modified member is the first to be executed (when the class is loaded), it does not depend on the object, but the inner class of the instance depends on the external class object (it will not be loaded when the class is loaded ) Ordinary member variables, there are static in the inner class of the instance, and static is created when the class is loaded ), so the inner class of the instance cannot be directly modified with static! ! !

It’s not impossible to use static, just add a final after the static (because the final modifier is a constant, no class loading is required, and the value will be known when compiling )

class OutClass1 {

    class InnerClass {

        public static final int data = 1;

    }
}

How to instantiate the inner class of the instance?

public class test1 {
    
    public static void main(String[] args) {

        OutClass1.InnerClass innerClass = new OutClass1().new InnerClass();
        
    }
}

How to access the external class members with the same name?

class OutClass1 {

    public int data = 1;

    class InnerClass {

        public int data = 2;

        public  void test() {

            //访问自己类中的data1
            System.out.println(data);
            System.out.println(this.data);

            //访问外部类的data
            System.out.println(OutClass1.this.data);
        }

    }

}

Guess you like

Origin blog.csdn.net/llt2997632602/article/details/130590803