non-static class cannot be referenced from a static context

Bin :

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 ?

Emil Sit :

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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

non static method cannot be referenced from a static context

From Dev

non-static variable s cannot be referenced from a static context

From Dev

non-static method getIntent() cannot be referenced from a static context

From Dev

non static variable this cannot be referenced from a static context

From Java

"non-static variable this cannot be referenced from a static context"?

From Dev

java: non-static variable this cannot be referenced from a static context

From Dev

Non-static variable filepath cannot be referenced from a static context

From Dev

Non static variable cannot be referenced from static context java

From Dev

non-static variable cannot be referenced from static context [JAVA]

From Dev

"Non-static method cannot be referenced from static context" error

From Dev

non-static variable cannot be referenced from a static context java

From Dev

Non-static edit() cannot be referenced from a static context

From Dev

non static setGravity cannot be referenced from static context

From Dev

Non-static variable cannot be referenced from a static context?

From Dev

Non-static method getSocketFactory cannot be referenced from a static context

From Dev

Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

From Dev

Cannot be referenced from a static context

From Dev

Why I can not make object of abstract class here and I got error of Non-static variable this cannot be referenced from a static context

From Dev

Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

From Java

'Main.this' cannot be referenced from a static context if outer class is generified

From Dev

Error:non static method 'edit' cannot be referenced in static context

From Java

Cannot be referenced from static context error

From Java

Java - cannot be referenced from a static context

From Dev

Notification `notify()` cannot be referenced from a static context

From Dev

Android - executePendingTransactions cannot be referenced from static context

From Dev

Map.merge .. non-static method cannot be referenced from static context

From Dev

non-static method matcher(CharSequence) cannot be referenced from a static context

From Java

Why does String::isEmpty works when non-static method cannot be referenced from a static context?

From Java

java - Non-static method 'getLogger' cannot be referenced from a static context

Related Related

  1. 1

    non static method cannot be referenced from a static context

  2. 2

    non-static variable s cannot be referenced from a static context

  3. 3

    non-static method getIntent() cannot be referenced from a static context

  4. 4

    non static variable this cannot be referenced from a static context

  5. 5

    "non-static variable this cannot be referenced from a static context"?

  6. 6

    java: non-static variable this cannot be referenced from a static context

  7. 7

    Non-static variable filepath cannot be referenced from a static context

  8. 8

    Non static variable cannot be referenced from static context java

  9. 9

    non-static variable cannot be referenced from static context [JAVA]

  10. 10

    "Non-static method cannot be referenced from static context" error

  11. 11

    non-static variable cannot be referenced from a static context java

  12. 12

    Non-static edit() cannot be referenced from a static context

  13. 13

    non static setGravity cannot be referenced from static context

  14. 14

    Non-static variable cannot be referenced from a static context?

  15. 15

    Non-static method getSocketFactory cannot be referenced from a static context

  16. 16

    Can't call constructor of inner class in a static context -- "non-static variable this cannot be referenced from a static context"

  17. 17

    Cannot be referenced from a static context

  18. 18

    Why I can not make object of abstract class here and I got error of Non-static variable this cannot be referenced from a static context

  19. 19

    Error: Non-static variable super cannot be referenced from a static context >>but i use static keyword

  20. 20

    'Main.this' cannot be referenced from a static context if outer class is generified

  21. 21

    Error:non static method 'edit' cannot be referenced in static context

  22. 22

    Cannot be referenced from static context error

  23. 23

    Java - cannot be referenced from a static context

  24. 24

    Notification `notify()` cannot be referenced from a static context

  25. 25

    Android - executePendingTransactions cannot be referenced from static context

  26. 26

    Map.merge .. non-static method cannot be referenced from static context

  27. 27

    non-static method matcher(CharSequence) cannot be referenced from a static context

  28. 28

    Why does String::isEmpty works when non-static method cannot be referenced from a static context?

  29. 29

    java - Non-static method 'getLogger' cannot be referenced from a static context

HotTag

Archive