Non static variable cannot be referenced from static context java

JaAnTr

I'm aware this question has been asked many, many times and I've read loads of answers but I can't get my head around how to fix my problem. Here is my code:

public class Circle
{
    public int diameter;
    public int xPosition;
    public int yPosition;
    public String color;


    public Circle()
    {
        diameter = 30;
        xPosition = 20;
        yPosition = 60;
        color = "blue";
    }

    public void toString()
    {
        System.out.println("The diameter of the circle is " + Circle.diameter);
        System.out.println("The x position of the circle is " + Circle.xPosition);
        System.out.println("The y position of the circle is " + Circle.yPosition);
        System.out.println("The colour of the circle is " + Circle.color);
    }


   public static void main(String[] args)
   {
        Circle c1 = new Circle();
        c1.toString();

   }

}

I did originally fix the issue by changing:

public class Circle
{
    public int diameter;
    public int xPosition;
    public int yPosition;
    public String color;
}

to

public class Circle
{
    public static int diameter;
    public static int xPosition;
    public static int yPosition;
    public static String color;
}

Whilst this does work, I feel it's not a good way to do it. I've read something about instance variables but I'm not sure how to do this.

Thanks.

Sage

The problem is with with toString() function:

    System.out.println("The diameter of the circle is " + Circle.diameter);
    System.out.println("The x position of the circle is " + Circle.xPosition);
    System.out.println("The y position of the circle is " + Circle.yPosition);
    System.out.println("The colour of the circle is " + Circle.color);

You are referencing non-static variable with Class reference. Just replace Circle with this. Have a look Understanding Instance and Class member tutorial

Edit:

Strangely, it's not letting me use the name toString() because "toString() in Circle cannot override toString() in object". It works with any other name though

yes because, toString() method is declared in Object class which is super class for all of the java class. This method has a return type with String, where instead you are using void. So either you will have to change the return type to String or change the function name to anything other.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

java: non-static variable this 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 a static context java

From Dev

non-static variable s 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

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

From Dev

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

From Java

Java Generics: non-static type variable T 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 Java

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

From Dev

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

From Dev

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

From Dev

Java JTextField and non-static variable 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 this cannot be referenced from a static content Java

From Dev

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

From Java

non static method cannot be referenced from a static context

From Dev

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

From Java

non-static class cannot be referenced from a static context

From Dev

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

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 method getSocketFactory cannot be referenced from a static context

From Java

Java - cannot be referenced from a static context

From Java

Why do I get "non-static variable this cannot be referenced from a static context"?

From Java

"Non-static variable this cannot be referenced from a static context" when creating an object

From Dev

Print at jTextArea using .setText returns error non-static variable cannot be referenced from static context

From Dev

non-static variable this cannot be referenced from a static context issue when instantiating a new object

Related Related

  1. 1

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

  2. 2

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

  3. 3

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

  4. 4

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

  5. 5

    non static variable this cannot be referenced from a static context

  6. 6

    "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 a static context?

  9. 9

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

  10. 10

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

  11. 11

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

  12. 12

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

  13. 13

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

  14. 14

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

  15. 15

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

  16. 16

    non-static variable this cannot be referenced from a static content Java

  17. 17

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

  18. 18

    non static method cannot be referenced from a static context

  19. 19

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

  20. 20

    non-static class cannot be referenced from a static context

  21. 21

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

  22. 22

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

  23. 23

    non static setGravity cannot be referenced from static context

  24. 24

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

  25. 25

    Java - cannot be referenced from a static context

  26. 26

    Why do I get "non-static variable this cannot be referenced from a static context"?

  27. 27

    "Non-static variable this cannot be referenced from a static context" when creating an object

  28. 28

    Print at jTextArea using .setText returns error non-static variable cannot be referenced from static context

  29. 29

    non-static variable this cannot be referenced from a static context issue when instantiating a new object

HotTag

Archive