shanwu
shanwu

Reputation: 1675

nested classes in java

Could anyone please tell me why the code below doesn't work ?

public class NestedClassPrac {
    public static class Nested {
        public void sayHello() { System.out.println("First nested class~");}
        public static class LittleNested {
            public void sayHello() { System.out.println("this is why we are nested class");}
        }
    }


    public static void main(String[] args) {
        Nested a = new Nested();
        a.sayHello();
        LittleNested b = new LittleNested();
        b.sayHello();
    }
}

error msg:

NestedClassPrac.java:13: cannot find symbol
symbol  : class LittleNested

location: class NestedClassPrac
        LittleNested b = new LittleNested();
        ^

NestedClassPrac.java:13: cannot find symbol

symbol  : class LittleNested

location: class NestedClassPrac
        LittleNested b = new LittleNested();
                             ^
2 errors

Upvotes: 0

Views: 132

Answers (5)

jacobhyphenated
jacobhyphenated

Reputation: 2145

The following will compile:

Nested.LittleNested b = new Nested.LittleNested();

or you can import LittleNested

import <yourpackage>.NestedClassPrac.Nested.LittleNested;

Basically, you have access inside main to anything at the same hierarchical level inside NestedClassPrac without needing an import. That gives you access to Nested. However, LittleNested is not at the same level hierarchically; LittleNested is inside Nested. Therefore, you need an import.

Upvotes: 1

carlosrogue
carlosrogue

Reputation: 11

Your code doesn't work because from the main method scope you need to refer to the LittleNested sub-inner class including the enclosing class name:

public class NestedClassPrac {
    public static class Nested {
        public void sayHello() { System.out.println("First nested class~");}
        public static class LittleNested {
            public void sayHello() { System.out.println("this is why we are nested class");}
        }
    }


    public static void main(String[] args) {
        Nested a = new Nested();
        a.sayHello();
        Nested.LittleNested b = new Nested.LittleNested();
        b.sayHello();
    }
}

From the main method only the Nested class can be referred. You can read about it at Nested Classes - Java Tutorial

Upvotes: 1

Rustam
Rustam

Reputation: 6515

you should access like this:

 OuterClass.InnerClass1.InnerClass2...InnerClassN obj=new OuterClass.InnerClass1.InnerClass2...InnerClassN();
obj.method();

Upvotes: 0

Akash Thakare
Akash Thakare

Reputation: 22972

LittleNested is only accessible through Nested class you can not access it directly without use of Nested.You can access inner static class same as you access any other static member of class (i.e, method,variable).

For Example

class X{
    static class Y{
        static class Z{
            Z(){
                System.out.println("Inside Z");
            }
        }
    }
}

you can create Object of Z like this as inner classes are static.

X.Y.Z obj=new X.Y.Z();

Upvotes: 2

Mikkel L&#248;kke
Mikkel L&#248;kke

Reputation: 3749

        Nested.LittleNested b = new Nested.LittleNested();

What exactly are you trying to do?

Upvotes: 3

Related Questions