Casting Class into String and Vice Versa in JAVA

Posted by topgun_ivard on Stack Overflow See other posts from Stack Overflow or by topgun_ivard
Published on 2010-04-15T04:03:50Z Indexed on 2010/04/15 4:13 UTC
Read the original article Hit count: 187

Filed under:

I have a program in which I need to store a Class object into memory by casting it into String. Is it possible to convert the String back into the original Class so that I can use that class variables? I am doing this in JAVA.

Example: test.java

class hello{
 public String h1;
 public String h2;
}
public class test {

 public static void main(String[] args)
 {
   hello h = new hello();

   h.h1 = "hello";
   h.h2 = "world";

   String s = h.toString();
   System.out.println("Print s : "+s);

   // Now I need to convert String s into type hello so that 
          // I can do this:
          // (hello)s.h1;
          // (hello)s.h2;   
 } 
}

NOTE: this is not a homework, this is a personal project and I would be grateful if anyone can help!

Thanks! Ivar

© Stack Overflow or respective owner

Related posts about java