The Artima Developer Community
Sponsored Link

Java Answers Forum
Initializing a class and naming it the contents of a String

4 replies on 1 page. Most recent reply: Mar 16, 2005 8:16 AM by Matt Gerrans

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Chris Miller

Posts: 23
Nickname: lordsauron
Registered: Jan, 2005

Initializing a class and naming it the contents of a String Posted: Feb 22, 2005 5:45 PM
Reply to this message Reply
Advertisement
I looked through the API, and tried a few ideas (unsuccessfully) to make a new object (you really don't need to know what the object was).

So, I had something equiviquol to this:
BufferedReader in=new BufferedReader(new InputStreamReader(System.in));
String szString=in.readLine();
GenericObject szString=new GenericObject(GenericJunk);


I want it to be named the contents of szString. How do I do this (I'm using Java2 1.5.something...)

Any help would be great!


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Initializing a class and naming it the contents of a String Posted: Feb 24, 2005 10:47 AM
Reply to this message Reply
I really don't get it:

You want to create a variable and give the variable the name of the string (forget it, variable names only serve to make the code readable but loose any importance at runtime)?

Or do you want to create a reference to a certain class wich has the same name as the String you read?

What you wrote here, just does not make any sense.
szString is allready defined as String. It can contain only instances of classes wich extend the class String.

perry anderson

Posts: 10
Nickname: anderson
Registered: Feb, 2005

Re: Initializing a class and naming it the contents of a String Posted: Feb 28, 2005 3:37 PM
Reply to this message Reply
> I looked through the API, and tried a few ideas
> (unsuccessfully) to make a new object (you really don't
> need to know what the object was).
>
> So, I had something equiviquol to this:
>
> BufferedReader in=new BufferedReader(new
> InputStreamReader(System.in));
> String szString=in.readLine();
> GenericObject szString=new GenericObject(GenericJunk);
> 

>
> I want it to be named the contents of szString. How do I
> do this (I'm using Java2 1.5.something...)
>
> Any help would be great!

either

 BufferedReader in=new BufferedReader(new
 InputStreamReader isr = new InputStreamReader(System.in));
 String szString=isr.readLine();
 GenericObject myObject =new GenericObject(szString);


or

 BufferedReader in=new BufferedReader(new
 InputStreamReader isr = new InputStreamReader(System.in));
 ObjectStreamReader osr = new ObjectStreamReader(isr));
 Object myObject=osr.readObject();


or

 BufferedReader in=new BufferedReader(new
 InputStreamReader isr = new InputStreamReader(System.in));
 ObjectStreamReader osr = new ObjectStreamReader(isr));
 GenericObject myObject=(GenericObject)osr.readObject();


or

 BufferedReader in=new BufferedReader(new
 InputStreamReader isr = new InputStreamReader(System.in));
 ObjectStreamReader osr = new ObjectStreamReader(isr));
 Object myObject = osr.readObject();
 if ( myObject instanceof GenericObject )
    GenericObject myGenericObject=(GenericObject)myObject;


but never never never

>
> BufferedReader in=new BufferedReader(new
> InputStreamReader(System.in));
> String szString=in.readLine();
> GenericObject szString=new GenericObject(GenericJunk);
> 


only an incredibly stupid person with almost no self esteem would write something like you did... you should be ashamed of yourself

tough

- perry

Joey

Posts: 4
Nickname: wittyman
Registered: Mar, 2005

Re: Initializing a class and naming it the contents of a String Posted: Mar 16, 2005 5:14 AM
Reply to this message Reply
I am sorry if I understood you wrong...

but If the problem that you are trying to work around is ... "How do I get the name of the class??" and so you are trying to create the class with that name so you can access it later, then this cannot be done.

The answer to the above question is use Class.forName(). Look up Class API for more.

Matt Gerrans

Posts: 1153
Nickname: matt
Registered: Feb, 2002

Re: Initializing a class and naming it the contents of a String Posted: Mar 16, 2005 8:16 AM
Reply to this message Reply
Not only that, but using Hungarian notation is anathema in Java. Especially when you don't even know what it is (the "sz" prefix would denote a zero-terminated string -- not a String object, since you don't even know whether or not it is zero-terminated).

If you want to dynamically create a class with a particular name, then you'll need to learn about something called "reflection" in Java. Even before that, it seems like you need to clarify exactly what it is you are trying to accomplish.

Flat View: This topic has 4 replies on 1 page
Topic: whether Robot class work faster tha EventQueue class in java Previous Topic   Next Topic Topic: More Free Stuff / Legit Sites?

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use