Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 726559
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T06:27:57+00:00 2026-05-14T06:27:57+00:00

I have a program in which I need to store a Class object into

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T06:27:57+00:00Added an answer on May 14, 2026 at 6:27 am

    I think what you want to do is Serialization. I’m confused by your comment:

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

    You can’t just cast String objects to arbitrary class types. Maybe you can elaborate on what you’re trying to accomplish here. If you want to be able to “save” a class to a file, then read it back in as an object, you want Serialization. Like this:

    import java.io.FileInputStream;
    import java.io.FileOutputStream;
    import java.io.ObjectInputStream;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    
    class Hello implements Serializable {
      public String h1;
      public String h2;
    
      @Override
      public String toString() {
        return "{ h1: " + h1 + ", h2: " + h2 + " }";
      }
    
      public static void main(String[] args) throws Exception {
        Hello h = new Hello();
    
        h.h1 = "hello";
        h.h2 = "world";
    
        ObjectOutputStream outstream = new ObjectOutputStream(new FileOutputStream("hello.ser"));
        outstream.writeObject(h);
    
        System.out.println("1) h: " + h);
    
        h = null;
    
        System.out.println("2) h: " + h);
    
        ObjectInputStream instream = new ObjectInputStream(new FileInputStream("hello.ser"));
        h = (Hello) instream.readObject();
    
        System.out.println("3) h: " + h);
    
      }
    }
    

    It can get more complicated when your fields are more complex classes than String. Implementing Serializable is just a “marker” interface that says that the object can be serialized, it doesn’t require any methods to be implemented. Your simple class just needs to be written out using an ObjectOutputStream and can be read back in using an ObjectInputStream.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We have some COBOL programs in our financial applications which need to interact with
I have a LAMP (PHP) web app which need to interface with programs on
I have a program which needs to behave slightly differently on Tiger than on
I have a program which deliberately performs a divide by zero (and stores the
I have a program which has some Textareas / Labels these can be anywhere
I have a program which needs installing on windows 64 boxes. Most of the
I have a program which only needs a NotifyIcon to work as intended. So
Consider this problem: I have a program which should fetch (let's say) 100 records
I have an AppleScript program which creates XML tags and elements within an Adobe
I have a program in which I've lost the C++ source code. Are there

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.