-1

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.

JaAnTr
  • 896
  • 3
  • 16
  • 28
  • 1
    Note that `public void toString()` is a very bad method name. You should override the `toString()` method from the `Object` class instead. I.e `public String toString()` and return a String that describe your object. – Alexis C. Dec 07 '13 at 15:25

2 Answers2

7

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.

Sage
  • 15,290
  • 3
  • 33
  • 38
  • Or just `diameter` etc., which is equivalent to `this.diameter`. – Patricia Shanahan Dec 07 '13 at 15:23
  • @PatriciaShanahan, thank you but isn't referencing the instance with `this` is preferable as a conventional approach. – Sage Dec 07 '13 at 15:25
  • @JaAnTr I suggest reading the API documentation for [Object](http://docs.oracle.com/javase/6/docs/api/java/lang/Object.html). All classes extend Object, directly or indirectly, so you cannot have a method with the same name and argument list as any of the Object methods unless it has the same return type. – Patricia Shanahan Dec 07 '13 at 15:29
  • Thanks everyone, going to read it all now. I understand it much better. – JaAnTr Dec 07 '13 at 15:30
  • @Sage I suppose some shops may require the use of `this.` for referencing fields even when there is no ambiguity, but it does not seem to be a very common convention. It is certainly not followed in the source code for java.lang and java.util classes. – Patricia Shanahan Dec 07 '13 at 15:33
0

Since Static variables and methods are connected to the class itself and not it's instances so they called using the class name like:

================================================================================

class ABC{
public static int a;

public void A()
{
   System.out.println("The value of a is: "+a);
}

public class test
{
   ABC.a = 10;
   ABC.A();
}

You need not to change variables to static variables.

Just call variables using this operator.

Surinder ツ
  • 1,778
  • 3
  • 16
  • 27