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....

Recommended Answers

All 13 Replies

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?

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.

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();
//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();
    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

Yeah I get that, but is not what I'm looking for. Thanks a lot of taking the time to do all this though.

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.

The closer you can get is this:

Try this:


Or this:

Table tbl= new Table();


Not sure why you are trying to do this but one approach that might work depending on your goal is to always set the component name by hand. So...

JCheckBox myCheckBox = new JCheckBox("Select Me");

Now the component has a name, that happens to be the symbolic name you gave it when you wrote your code.

You can use it like this:

HashMap<String, JCheckBox> checkBoxes = new HashMap<String, JCheckBox>();

JCheckBox myCheckBox = new JCheckBox("Select Me");

JCheckBox yourCheckBox = new JCheckBox("Select You");

checkBoxes.add(myCheckBox.getName(), myCheckBox);
checkBoxes.add(yourCheckBox.getName(), yourCheckBox);

String interestingCheckBox = new String("myCheckBox");
JCheckBox newCheckBox = checkBoxes.get(interestingCheckBox);

I use this technique to bulk manage check boxes, radio buttons etc on preference pages. By adding each component to a ArrayList I can then easily iterate over the list and write the user choices to the preferences file with 3 lines of code. I can equally read from that file and update the preference page in a simple iterator.

This question has nothing to do with Swing.

this is a little late response-wise, but here is a very simple way to do this:

public static void main (String[] args)
  Table one = new Table("Table one");

create a private string variable and change your constructor to accept a string for the table name and you are good to go!

You're two years too late on this one.

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

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.

Okay. Closing.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.