Java - cannot be referenced from a static context

Firkamon

Here's my simple class:

public class Project {
    private int size;
    private Obj tree;
    static Obj insert( Obj t, String s ) { // t is null
        t = new Obj();
        t.val = s;
        return t;
    }
    public Project()
    {
      Obj tree = new Obj();
      int size=0;
    }
    public class Obj
    {
      public String val;
      public Obj()
      {
        val=null;
      }
    }     
}

However, when I try to create a new object in the insert() function, I get this error:

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

Your Obj class is not static == it's an inner class. This means that it needs an instance of the enclosing class Project to live.

From the static method insert, there is no such Project instance, hence the compiler error.

The Obj class doesn't seem to need any instance variables in Project, so there is no reason to keep it non-static. Make the Obj class static in Project.

public static class Obj

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Java

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

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Java

Non-static method cannot be referenced from a static context in java 8 streams

From Java

non-static class cannot be referenced from a static context

From Java

Java Generics: non-static type variable T cannot be referenced from a static context

From Java

non static method cannot be referenced from a static context

From Java

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

From Java

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

From Java

Cannot be referenced from static context error

From Dev

Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context

From Dev

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

From Dev

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

From Dev

Rectangle.java:35: error: non-static variable this cannot be referenced from a static context

From Dev

error: non-static variable scan cannot be referenced from a static context in Java

From Dev

Non static variable cannot be referenced from static context java

From Dev

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

From Dev

Android - executePendingTransactions cannot be referenced from static context

From Dev

non static variable this cannot be referenced from a static context

From Dev

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

From Dev

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

From Dev

Java JTextField and non-static variable cannot be referenced from a static context

From Dev

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

From Dev

Java Language | Error :- non-static variable scan cannot be referenced from a static context

From Dev

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

From Dev

Java Blackjack program creates error: non-static variable this cannot be referenced from a static context

From Dev

JAVA - non-static method add(E) cannot be referenced from a static context

From Dev

Cannot be referenced from a static context

From Dev

Non-static method cannot be referenced from a static context when flattening map java 8

From Dev

Non-static method cannot be referenced from a static context - Java to Kotlin - Android

Related Related

  1. 1

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

  2. 2

    Non-static method cannot be referenced from a static context in java 8 streams

  3. 3

    Non-static method cannot be referenced from a static context in java 8 streams

  4. 4

    non-static class cannot be referenced from a static context

  5. 5

    Java Generics: non-static type variable T cannot be referenced from a static context

  6. 6

    non static method cannot be referenced from a static context

  7. 7

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

  8. 8

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

  9. 9

    Cannot be referenced from static context error

  10. 10

    Non-static method 'getSharedPreferences (java.lang.String, int)' cannot be referenced from a static context

  11. 11

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

  12. 12

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

  13. 13

    Rectangle.java:35: error: non-static variable this cannot be referenced from a static context

  14. 14

    error: non-static variable scan cannot be referenced from a static context in Java

  15. 15

    Non static variable cannot be referenced from static context java

  16. 16

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

  17. 17

    Android - executePendingTransactions cannot be referenced from static context

  18. 18

    non static variable this cannot be referenced from a static context

  19. 19

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

  20. 20

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

  21. 21

    Java JTextField and non-static variable cannot be referenced from a static context

  22. 22

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

  23. 23

    Java Language | Error :- non-static variable scan cannot be referenced from a static context

  24. 24

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

  25. 25

    Java Blackjack program creates error: non-static variable this cannot be referenced from a static context

  26. 26

    JAVA - non-static method add(E) cannot be referenced from a static context

  27. 27

    Cannot be referenced from a static context

  28. 28

    Non-static method cannot be referenced from a static context when flattening map java 8

  29. 29

    Non-static method cannot be referenced from a static context - Java to Kotlin - Android

HotTag

Archive