How to write toString() without knowing for which variable it is

Viewed 41

I have a class that runs like this:

import java.util.Random;
public class Rennschnecke
{
public static String name;
public static String rasse;
public static double max_vel;
public static double way_total;

Random random = new Random();

static Rennschnecke rs = new Rennschnecke(name, race, max_vel, way_total);

public Rennschnecke(String n, String r, double m, double w)
{
    n = name;
    r = race;
    m = max_vel;
    w = way_total;
}

public Rennschnecke()
{
    
}


public static void main(String[] args)
{
    // TODO Auto-generated method stub
    rs.move(10);
    rs.toString();
}

public void move(double max)
{
    double tempo = -1; 
    while(tempo <= 0 || tempo >= max)
    {
        tempo = random.nextDouble(max);
    }
    System.out.println(tempo);
}

/*public String toString()
{
    return "";
}*/
}

Problem is, I'm stuck at the toString() method. I want it to return the String value it is attached to (like String x = strvar.toStr();), but I don't want it to be limited to one variable by doing something like public static String thisX = "ABC"; // other code // public String toString(){return thisX} (that's only pseudo code). It should go like String a = "ABC"; String b = "XXX"; String c = "Test"; // other code // String x = a.toString(); String y = b.toString(); String z = c.toString(); or something in that direction. toString should work on the String it's attached to with the ..

Any idea how this could work?

0 Answers
Related