package my_package;

my_object obj_instance;
String my_string;

my_string = obj_instance.toString();

System.out.println(my_string) ;
// gives  my_package.my_object@asj128

// I've lost track of obj_instance but it in memory 
// and I want to recover it from my_string , like

my_object obj_instance1
obj_instance1 = ( my_object) my_string ;  

some thing like string to object converstion
how can we do this ?
obviously above typecasting doesn't work in Java :)

I simplu doesn't work like that. If you want a reference to an object, use a reference variable of the right kind. Java references are "opaque" - you can use them but you can't manipulate them. There's no way to turn a String into an object reference.

If you want to track objects according to some related String value, you can use a Map with the strings as the keys, and the objects as the values.

what you could do, is write some sort of factory-like method.

for instance:

public class Person{
  private String name;
  private int age;

  public Person(String name, int age){
    this.name = name;
    this.age = age;
  }

  // just assume the setters and getters are here.

}

at this point, there is no way to just "read a line from a file", with the data needed, and instantiate a person.

Let's say, we add a new method to the Person class (one that can also be in any other class) which would accept a String (a line out of a text file) and instantiate a Person based on this.

let's assume, that a txt file contains:
Mark-15
Sophie-20

we would have something like this:

public class Person{
  private String name;
  private int age;

  public Person(String name, int age){
    this.name = name;
    this.age = age;
  }

  // just assume the setters and getters are here.

  public static Person createPerson(String line){
      String[] data = line.split("-");
      return new Person(data[0], Integer.parseInt(data[1]));
  }

}

The javadoc, about toString method say:

 toString
public String toString()
Returns a string representation of the object. In general, the toString method returns a string that "textually represents" this object. The result should be a concise but informative representation that is easy for a person to read. It is recommended that all subclasses override this method.
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())

Returns:
a string representation of the object.

Hello, here an example, an explanation about your problem:

package daniweb;

/**
 *
 * @author JoZulyk http://inovawebstudio.com
 */
public class My_object {
    private int num;
    private int value;

    public int getNum() {
        return num;
    }

    public int getValue() {
        return value;
    }

    public void setNum(int num) {
        this.num = num;
    }

    public void setValue(int value) {
        this.value = value;
    }
}

now the main class:

package daniweb;

/**
 *
 * @author JoZulyk http://inovawebstudio.com
 */
public class MainMy_Object {
    public static void main(String argv []){
        My_object obj_instance = new My_object();
        //Here obj_instance have no data added, only created by default
        String my_string = obj_instance.toString();
        //printing the value of this in toString();
        System.out.println("String value: " + my_string);
        //putting values on this obj
        obj_instance.setNum(10);
        obj_instance.setValue(15);
        my_string = obj_instance.toString();
        //here u can show that added date doesnot change nothing in obj_instance.toString is the same
        //then, it(my_string) have not information about values stored, changed; is only an "textually representation"
        //u can't retrieve information only with the toString (my_string) representation.
        System.out.println("String value after added data: " + my_string); 
    }
}

... for more information read about toString(), hashCode(), equals() from the Object class. And consider JamesCherrill comment. Using maps with unique key values stored (maybe in strings)... etc
Is all.

zolymo, yes, but you are referring to the toString method of the Object class, while most classes will have their own implementation.
the object's method just points to the memory address, and, indeed, this doesn't change once you change some value of the object, depending on how you make the change, but in most other cases, it will.

i refer this because, nataraja833 use toString() without implementation (using toString() of the parent Class - Object).

@stultuske @zolymo
I dont want to retreive data based on string .

background
I've a ArrayList of size 10-20million objects and when user queries for particular type of data , I've to return some of these objects as handles in string format ( Java/TCL inteface )

and in future if the user gives these handles , I should be able to know from which object this handle was created .

Ex : in C/C++ I've directly used objects pointers address as handle
returned address in hex format and I was able to typecast it .

@JamesCherrill Yes using map is one way but we are talking about 10-20 million objects , this is a huge memory over head
my application is already taking 2GB Ram without this :)

The map entries are just a pair of references (pointers), so the map will be a lot smaller than the existing data. Depending on how your ArrayList is updated maybe you can just use the numeric index into the ArrayList?
Java (deliberately) has no facilities for working with memeory addresses, so your C/C++ approach simply won't work.

Depending on how your ArrayList is updated maybe you can just use the numeric index into the ArrayList?

looks like this is the only way out

Thanks

Thanks All ,
I'm going with Indexing :)

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.