45

Possible Duplicate:
Why do I get “non-static variable this cannot be referenced from a static context”?

Here are the codes

public class Stack
{
    private class Node{
        ...
    }
    ...
    public static void main(String[] args){
         Node node = new Node(); // generates a compiling error
    }
}  

the error says:

non-static class Node cannot be referenced from a static context

Why shouldn't I refer the Node class in my main() method ?

Community
  • 1
  • 1
Bin
  • 980
  • 2
  • 12
  • 24

6 Answers6

64

A non-static nested class in Java contains an implicit reference to an instance of the parent class. Thus to instantiate a Node, you would need to also have an instance of Stack. In a static context (the main method), there is no instance of Stack to refer to. Thus the compiler indicates it can not construct a Node.

If you make Node a static class (or regular outer class), then it will not need a reference to Stack and can be instantiated directly in the static main method.

See the Java Language Specification, Chapter 8 for details, such as Example 8.1.3-2.

Paul Bellora
  • 54,340
  • 18
  • 130
  • 181
Emil Sit
  • 22,894
  • 7
  • 53
  • 75
  • 7
    `Node x = new Stack().new Node();` is another way to solve this problem, but I find that when I make the **Stack** class **generic** -- Stack, and write code like: `Node x = new Stack().new Node();`, it it's wrong again with the same error message. Do you know why? – Bin Nov 14 '12 at 10:25
  • @zbjosh Try typing `x` as `Stack.Node`. – Paul Bellora Nov 14 '12 at 19:48
  • @PaulBellora , thanks, it works!! But...what's the problem in it – Bin Nov 15 '12 at 06:34
  • @Bin The problem is that every time you want a single Node object you are instantiating an entire Stack, which is a waste of resources and garbage collection. – EntangledLoops Mar 31 '15 at 23:47
13

Non-static nested classes (inner classes) have access to other members of the enclosing class, even if they are declared private. Static nested classes do not have access to other members of the enclosing class. As a member of the OuterClass, a nested class can be declared private, public, protected, or package private.

To instantiate an inner class, you must first instantiate the outer class. Then, create the inner object within the outer object with this syntax : OuterClass.InnerClass innerObject = outerObject.new InnerClass();

public static void main(String[] args){
         Stack stack = new Stack();
         Stack.Node node = new Stack().new Node();
    }

or

public class Stack
{
    private class Node{
        ...
    }
    ...
    ...
    ...  

    public static void main(String[] args){
             Node node = new Stack().new Node(); 
    }
}  
Subhrajyoti Majumder
  • 40,646
  • 13
  • 77
  • 103
3

Or you could declare the class Node outside of public class Stack

Like so,

    public class Stack
    {

        public static void main(String[] args){
             Node node = new Node();         
}        
    }  
    class Node{

    }
Austin
  • 4,801
  • 6
  • 34
  • 54
3

Java has two types of nested member classes: static and non-static (aka inner). The Node class is a non-static nested class. In order to create an instance of a Node you must have an instance of a Stack:

Stack s = new Stack();
Node n = s.new Node();
Andrey
  • 8,882
  • 10
  • 58
  • 82
2

Make your (Node) class static

private static class Node {
Bhesh Gurung
  • 50,430
  • 22
  • 93
  • 142
  • 1
    I know it, but can you explain it in detail ? – Bin Nov 14 '12 at 06:05
  • You cannot access non-static members from static context. The main method is static while the Node is non-static. So, you can either use an instance of outer class to instantiate the non-static inner member class. But, in this case I think it would make more sense to make it static. You just need one definition of the Node which can stick with the outer "class" and you will still be able instantiate it whenever need without the need to have an instance of the outer class. – Bhesh Gurung Nov 14 '12 at 06:08
1

If you use Eclipse IDE, you would see the explanation when you hover over the error. It should say something like this:

No enclosing instance of type Stack is accessible. Must qualify the allocation with an enclosing instance of type Stack (e.g. x.new A() where x is an instance of Stack).

Here is working code

public static void main(String[] args){
    Stack stack = new Stack();
     Node node = stack.new Node(); 
}
vandershraaf
  • 905
  • 3
  • 11
  • 23