0

Here is my sample code. This prints out "{test=theClass@7096985e}" but I need it to give me the values of type and scope. I have tried several things--any direction would be awesome. Thanks!

import java.util.*;

class theClass {
    String type;
    String scope;

    public theClass(String string1, String string2) {
        type = string1; scope = string2;
    }

}

public class Sandbox {

    public static void main(String[] args){
        Hashtable<String, theClass> theTable = new Hashtable<String, theClass>();
        theClass object = new theClass("int", "global");
        theTable.put("test", object);

        System.out.println(theTable.toString());

    }
}
2
  • Overrides toString() Commented Jul 8, 2013 at 8:01
  • 1
    Some comments on the code not related to your question. 1. Class names start with a capital letter by definition. (TheClass instead of theClass) This is makes it more readable for other people. 2. Use HashMap instead of Hashtable. While Hashtbale works fine, it is an outdated implementation which is slower than HashMap. 3. Don't name your parameters string1, string2, etc. Give them names like "scope" and "type". This makes it easier to understand the purpose when you method is called, by someone else. Commented Jul 8, 2013 at 8:10

4 Answers 4

5

Just override the toString method in your class.

class theClass{
    String type;
    String scope;

    public theClass(String string1, String string2)
    {
        type = string1; scope = string2;
    }

    @Override
    public String toString(){
      return type+" "+scope;
    }

}
Sign up to request clarification or add additional context in comments.

Comments

1

Add a Method toString() to your theClass{} e.g.

@Override
public String toString() {
    return "theClass {type=" + type+", scope= "+scope+"};
}

Comments

1

You need to override the default implementation of toString() method provided by the Object class in your class.

@Override
public String toString() {
   return "type=" + type+", scope= "+scope;
}

System.out.println() uses String.valueOf() method to print objects , which uses the toString() on the object . If you haven't overridden the toString() method in your class then it will invoke the default implementation provided by the Object class , which says :

The toString method for class Object returns a string consisting of the name of the class of which the object is an instance, the at-sign character `@', and the unsigned hexadecimal representation of the hash code of the object. In other words, this method returns a string equal to the value of:

getClass().getName() + '@' + Integer.toHexString(hashCode())

Hence you get such an output.

Comments

0

Your code works correctly. You indeed get your object from hash table. You are confused with the string representation of your object.

To show internal data you have to override default implementation of public String toString() method.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.