简体   繁体   中英

are the members of a static inner class by default static in java

public static class ViewHolder {

        public int a,b;
            public void method();


    }

are the method and the variables a and b by default static when I declare the class as static or do I have to separately declare them static ? I know its a noobish question but I am a little confused right now :(

No, they're not static by default, they're normal instance members.

Static inner classes, unlike normal inner classes, can have static members, though, if you explicitly declare them.

No, when you declare an inner static class you specify that the declaration itself is static , so that you don't need an object instance of the parent class to access it.

Nothing regarding inner members is involed.

The members of the Static nested class are not static. static keyword is specified with the class which signifies that the nested class can be instantiated with the containing outer class similar to static data member.

BaseClass.StaticNestedClass nestedClass = new BaseClass.StaticNestedClass();
nestedClass.nonStaticMethod();//correct
BaseClass.StaticNestedClass.nonStaticMethod()//Error

This has no effect on the data members of the static nested class which behave as normal class.

Please note if a static keyword is associated with a class then the class has to be a nested class

A public static class works just like any other class. The only real difference is that it is accessed through the containing class:

OuterClass.InnerClass foo = new OuterClass.InnerClass();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM