Possible to print an object's name?
Hi, suppose I have something like this on my main method.
public static void main (String[] args){
Table one = new Table();
}
How can I print the name of the object table?
Its so that I can print something like this
Table one:
Color is ...
Texture is....
etc
MrDiaz
Junior Poster in Training
59 posts since May 2006
Reputation Points: 10
Solved Threads: 2
You can create get/set methods inside the table class to set data and get data. What you could do is create a method that returns all the data you want, using the same basic structure as below.
An example:
public class Table{
String color = "red";
public void setColor(String color){
this.color = color;
}
public String getColor(){
return color;
}
}
Is that kind of what you were asking?
jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
No, is not what I'm looking for. I don't want to print or return variable names of the object table. I want to print the actual object name. Example.
I can create a new table named 'two' or 'three'. Two and Three are still table objects, each one has its own set of properties and such.
MrDiaz
Junior Poster in Training
59 posts since May 2006
Reputation Points: 10
Solved Threads: 2
In the Table class you could override the toString() method and have it return anything you want. For instance:
public class Table{
public String toString() {
return "This is a table ";
}
}
Table one = new Table();
System.out.println(one.toString());
//Outputs "This is a table"
However if you want to get information about a Table object you would need get and set methods like I mentioned earlier. I have a more in depth example from what I said earlier.
public class Table {
String color,texture;
public void setColor(String setColor){
this.color = setColor;
}
public String getColor(){
return color;
}
public void setTexture(String setTexture){
this.texture = setTexture;
}
public String getTexture(){
return texture;
}
}
public class TableTest {
public static void main(String[] args) {
Table one = new Table();
Table two = new Table();
one.setColor("red");
two.setColor("green");
one.setTexture("wood");
two.setTexture("plastic");
System.out.println("The color of table one is " + one.getColor());
System.out.println("The texture of table one is " + one.getTexture());
System.out.println("The color of table two is " + two.getColor());
System.out.println("The texture of table two is " + two.getTexture());
System.out.println("The class is " + one.toString());
}
}
The TableTest class outputs
The color of table one is red
The texture of table one is wood
The color of table two is green
The texture of table two is plastic
jasimp
Senior Poster
3,623 posts since Aug 2007
Reputation Points: 533
Solved Threads: 53
Yeah I get that, but is not what I'm looking for. Thanks a lot of taking the time to do all this though.
MrDiaz
Junior Poster in Training
59 posts since May 2006
Reputation Points: 10
Solved Threads: 2
No there isn't, because it doesn't have a name. The only thing you can do, is store the table in a Map and use the key that it is stored under as the "name". A variable is simply a way for the programmer to keep track of the reference. In the compiled program it does not exist in that form.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
The closer you can get is this:
Try this:
System.out.println(Table.class);
Or this:
Table tbl= new Table();
System.out.println(tbl.getClass());
System.out.println(tbl.getClass().getName());
Class
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
This question has nothing to do with Swing.
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494
You're two years too late on this one.
NormR1
Posting Expert
6,677 posts since Jun 2010
Reputation Points: 1,138
Solved Threads: 656
I THINK while printing the objet we wil get the answer in console classname with address of the object so
classname itself the object name
First of all that number is not the address of the object. Don't post wrong things that you don't know.
And this thread is way too old and your post doesn't contribute anything. Will some administrator close this thing.
javaAddict
Nearly a Senior Poster
3,329 posts since Dec 2007
Reputation Points: 1,014
Solved Threads: 448
masijade
Industrious Poster
4,253 posts since Feb 2006
Reputation Points: 1,471
Solved Threads: 494