Inner versus nested classes Java Interview Question

来源:互联网 发布:2017淘宝微淘抢红包 编辑:程序博客网 时间:2024/06/18 11:12

Guidewire 电话面试题

Reference from: http://www.programmerinterview.com/index.php/java-questions/inner-vs-nested-classes/

What is the difference between an inner and nested class in Java? What about the difference between an inner class and a static inner class?

This tutorial is a bit complex, but we’ll try to keep things as simple as possible. With that said, let’s start off with definition and explanation of nested classes.

Nested classes can be either static or non-static

Nested classes can be further classified into two different types of classes: non-static nested classes and static nested classes. Non-static nested classes are more formally known as inner classes. So, think of nested classes as a big container with 2 smaller boxes inside – 1 box is for static nested classes, and another box is for inner classes (also known as non-static nested classes).

Example of an inner class (aka non static nested class)

Here’s a simple example of an inner class – where InnerClass is the inner class:

class OuterClass  {/*  some code here...*/class InnerClass  {  }/*  some code here...*/}

Note in the code above that InnerClass is literally declared inside the OuterClass class.

Inner classes are subsets of nested classes


Remember that an inner class is a specific type of nested class that occurs when a nested class is non-static. And that is the “difference” between an inner class and a nested class – in other words, inner classes are subsets of nested classes. So, be careful, because the terms “inner class” and “nested class” are NOT interchangeable. You can say that an inner class is also a nested class, but you can NOT say that a nested class is also an inner class. This is because nested classes are part of the larger set that includes both inner classes and static nested classes.

What’s so special about inner classes?

So, what exactly is special about inner classes? Well, the main thing that you must remember about inner classes is that an instance of an inner class has access to all of the members of the outer class, even those that are marked “private”. So, when an instance of the inner class is created, there are no issues with having the inner class access the instance variables of the outer class.

Inner class versus static inner classes

Before we dive into the differences between inner classes and static inner classes, the most important thing you should know is that static inner classes is the wrong terminology – they should be called static nested classes instead.

Why you must use static “nested” classes instead of static “inner” classes

There is no such thing as a static inner class, because the term “inner class” means that the inner class has access to the instance variables of the outer class.

An inner class is part of the “inner circle” of the outer class

Think of an inner class as being part of the “inner circle” of the outer class – an inner class instance can access all the members of the outer class, even the ones declared private. But, if an inner class were also declared to be static it would be impossible to have access to all of the members of the outer class – think about why on your own before you read our answer. You should be able to come up with an answer on your own as long as you know what static means.

Inner classes have access even to non-static members of outer class

The reason an inner class can’t be static is because of the fact that it’s impossible to access non-static variables and methods from within a static context. So, a static inner class would only have access to the static members of the outer class. And, by definition, an inner class should have access even to the non-static members of the outer class. So, instead of calling them static inner classes, we call them static nested classes, and remember that the reason is that ‘inner’ classes have a special relationship with the outer class. And to call it a static inner class would be a misuse of the terminology – so they are called static nested classes instead.

Example of a static nested class

Here’s a simple example of a static nested class:

class Outer {    static class NestedStatic { }}

The correct way of thinking about the NestedStatic class in our example above is that it is a static member of the outer class. And because it is a static member, it means that it can be accessed without an instance of the Outer class.

How to instantiate a static nested class

The syntax used to instantiate a static nested class is different depending on whether the nested static class is a member of the current class or if the static nested class is nested in some other class. Confused? Some examples will help clarify what we mean:

Instantiating a static nested class from a non-enclosing class

Suppose we want to create an instance of a static nested class from another class. Here is some code where we do that – note that in the NonEnclosingClass class we instantiate the static class called Nested that is a member of the EnclosingClass class.

class EnclosingClass {  static class Nested {  void someMethod() { System.out.println("hello"); }  }}class NonEnclosingClass {public static void main(String[] args) {/*instantiate the Nested class that is a static  member of the EnclosingClass class:*/EnclosingClass.Nested n = new EnclosingClass.Nested(); n.someMethod();  //prints out "hello"}}

Instantiating a static nested class from an enclosing class

Here you can see an example of how to instantiate a static inner class that is already a part of the current class – note the more ‘normal’ syntax that you’re probably used to seeing of “Nested n = new Nested();” versus “EnclosingClass.Nested n = new EnclosingClass.Nested();” which is what was used to instantiate a static class that’s a member of a different class, as we showed in the example above.

class EnclosingClass {static class Nested {void anotherMethod() { System.out.println("hi again"); } }public static void main(String[] args) {//access enclosed class:Nested n = new Nested(); n.anotherMethod();  //prints out "hi again"}}

Accessing non-static instance variables from a static nested class

Now, let’s see what happens if we try to access some non-static instance variables that belong to the Outer class from inside of the NestedStatic class:

class Outer {    int instanceVar = 5;        static class NestedStatic {    public static void main(String[] args){        /*instanceVar is a non-static variable    belonging to the Outer class:  */        instanceVar = 10;         }    }}

The code above throws a compiler error saying “non-static variable instanceVar cannot be referenced from a static context”. That error makes perfect sense because we are trying to access instanceVar, which is non-static, from the NestedStatic class, which is static. And, because of the fact that we can access the static NestedStatic class without an object of the Outer class, it makes sense that we should not be able to access an instance variable like instanceVar (which needs an object of the Outer class in order to be accessed since it’s not static).

Conclusion

If you are looking for another interesting article on inner classes, then check out: When to use inner classes in Java

0 0
原创粉丝点击
热门问题 老师的惩罚 人脸识别 我在镇武司摸鱼那些年 重生之率土为王 我在大康的咸鱼生活 盘龙之生命进化 天生仙种 凡人之先天五行 春回大明朝 姑娘不必设防,我是瞎子 快来完事游泳了怎么办 游泳时来月经了怎么办 经期第7天游泳了怎么办 来月经已经游了泳怎么办 月经来了要游泳怎么办 三个月宝宝趴着不会抬头怎么办 我的月经不完怎么办 游泳时怎么办能浮出水面 游泳时眼镜起雾怎么办 练瑜伽手臂变粗怎么办 孕妇喝了芬达怎么办 宫口开了但头高怎么办 整天坐着肚子越来越大怎么办 坐久了屁股变大怎么办 屁股久坐的黑印怎么办 练瑜伽小腿变粗怎么办 练瑜伽腿粗了怎么办 练完瑜伽腿粗怎么办 肚子大屁股大腿粗怎么办 甲状腺偏大弥漫性欠均匀怎么办 大腿后侧拉伤了怎么办 腰和臀部都疼怎么办 瑜伽后弯后腰疼怎么办? 吃了大蒜肝痛怎么办 练瑜伽前躯不行怎么办 月子里碰冷水了怎么办 月子手碰凉水了怎么办 月子里碰了冷水怎么办 顺产后下面会痛怎么办 产后肚子瘦不下来怎么办 出月子后臀部很疼怎么办 做月子时臀部疼怎么办 月子臀部大腿疼困怎么办 产后抱孩子腰疼怎么办 剖腹产后5年小肚子很大怎么办 压力大工作忙瘦了怎么办? 哺乳乳房一大一小怎么办 哺乳期乳房一大一小怎么办 上班之后奶少了怎么办 出了月子还流恶露怎么办? 剖腹产2年刀疤痒怎么办