---
title: "How to return different values toString?"  
description: "How to return different values toString?"  
author: "Royce Roy"  
published: 2014-11-05  
updated: 2014-11-05  
canonical: https://www.mindstick.com/forum/2517/how-to-return-different-values-tostring  
category: "java"  
tags: ["java"]  
reading_time: 3 minutes  

---

# How to return different values toString?

I'm pretty new in [java](https://www.mindstick.com/articles/12214/web-development-company-in-india-laid-on-the-foundation-of-concrete-java-programming) and I'm doing a [simple](https://yourviews.mindstick.com/story/1469/5-simple-ways-to-stay-fit-amp-healthy) [program](https://www.mindstick.com/blog/12337/scaling-up-your-mentorship-program) but I don't know why I get different [values](https://www.mindstick.com/forum/327/sum-textbox-values), i.e., if I use getX, getY and getZ I get (6,5,8) but if I use [toString](https://www.mindstick.com/interview/2259/what-is-the-difference-between-tostring-convert-tostring) I get different values for X and Y (3, 4, 8), so can [anyone explain](https://www.mindstick.com/forum/34220/can-anyone-explain-me-about-off-page-activity) me why it happens because as far as I understand it should get the same values in both cases or what I'm [doing wrong](https://answers.mindstick.com/qa/36736/what-are-indians-doing-wrong-on-whatsapp)?\

```
public class Coordinates {     private double coorX, coorY;     Coordinates()    {        coorX = 1;        coorY = 1;    }     Coordinates(double x, double y)    {        coorX = x;        coorY = y;          }     void setX(double x)    {        coorX = x;     }     void setY(double y)    {        coorY = y;     }     double getX()    {        return coorX;    }     double getY()    {        return coorY;    }     public String toString()    {        String myString = "(" + coorX + " , " + coorY + ")";        return myString;    }     public class Coordinates3D extends Coordinates{        private double coorZ;         Coordinates3D()        {            super();            coorZ = 1;        }         Coordinates3D(double x, double y, double z)        {            super(x,y);            coorZ = z;        }         public void setZ(double z)        {            coorZ = z;        }         double getZ()        {            return coorZ;        }         @Override        public String toString()        {            String myString = "(" + coorX + " , " + coorY + " , " + coorZ + ")" ;            return myString;         }     }      /**     * @param args     */    public static void main(String[] args) {        // TODO Auto-generated method stub         Coordinates test1 = new Coordinates(3,4);          System.out.println(test1.toString());         System.out.println(test1.getX());        System.out.println(test1.getY());          Coordinates3D test2 = test1.new Coordinates3D(6,5,8);         System.out.println(test2.toString()); ---> here is the problem         System.out.println(test2.getX());        System.out.println(test2.getY());        System.out.println(test2.getZ());      } }
```

## Replies

### Reply by Takeshi Okada

First, there is a problem on how you define the visibility of the fields of the super class:

```
public class Coordinates {
    private double coorX, coorY;
```

This is that you cannot invoke super.coorX nor super.coorY on any subclass e.g. Coordinates3D. So, in toString method, when you have this code:

```
String myString = "(" + coorX + " , " + coorY + " , " + coorZ + ")" ;
```

It compiles and runs fine because Coordinates3D is an inner class. So, when using coorX here it's accessing to the value of coorX field stored in the instance of Coordinates class that created the instance of Coordinates3D. This can be easy to replicate if you separate the classes:

```
class Coordinates {
    private double coorX, coorY;
}

public class Coordinates3D extends Coordinates {
    @Override
    public String toString() {
        String myString = "(" + coorX + " , " + coorY + " , " + coorZ + ")" ;
        return myString;
    }
}
```

The best solution would be:

- mark the fields in the super class as protected
- separate the classes

If you still want to keep Coordinates3D as inner class (not recommended), then:

- mark the fields in the super class as protected
- use super.coorX and super.coorY to not have the same unexpected behavior.


---

Original Source: https://www.mindstick.com/forum/2517/how-to-return-different-values-tostring

Copyright © MindStick Software Pvt. Ltd. This Markdown version is provided for developers, AI systems, and offline reading.
