This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Java in General and the fly likes renaming object and creating object in run time Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "renaming object and creating object in run time" Watch "renaming object and creating object in run time" New topic
Author

renaming object and creating object in run time

krishna balaji
Ranch Hand

Joined: Mar 16, 2006
Posts: 31


what should i do if i want to rename "obj" to some other say name "obj1"

and is there any way to create name object in run time ?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

Originally posted by krishna balaji:


what should i do if i want to rename "obj" to some other say name "obj1"



No, you can NOT rename a reference variable. Rather, you can set a new reference variable to point to the same object and by setting this old reference variable to null optionally.


and is there any way to create name object in run time ?


All objects are created at runtime only. what do you mean by "name object"?


Everything has got its own deadline including one's EGO!
[CodeBarn] [Java Concepts-easily] [Corey's articles] [SCJP-SUN] [Servlet Examples] [Java Beginners FAQ] [Sun-Java Tutorials] [Java Coding Guidelines]
krishna balaji
Ranch Hand

Joined: Mar 16, 2006
Posts: 31
what i actually tried to ask is that i want to name variable at dynamically
for instance where s is the variable name
i want s (variable name) to b got from
any way?

and whats the use of naming and binding classes?
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

I am not quite sure but the Reflection API could help you!


..and whats the use of naming and binding classes?


It would be better if you can make use of One Thread Per Question.

Please do stick to the rules of the forum. If the questions are related to each other you can make them a part of a single thread.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
Sounds like a beginner's question. And Raghavan Muthu is right; you ought to start a new thread about bindings.

Remember that args as passed to the main method is an array of String, and s is a String. All you need to do to assign one of the Strings in args is to extract it from the array. Use the usual techniques for array member access.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
I don't think you can get the variable name from anything at Runtime. The nearest I can think of at the moment would be use that String as a key in a Map.
Arnold Reuser
Ranch Hand

Joined: Nov 20, 2003
Posts: 194
A solution that will provide you the ability to analyze and manipulate your code at runtime is e.g. BCEL
BCEL can extract the required information, and to answer your question.
Take a closer look at e.g local variable and field When this is the type of information you required, BCEL can resolve your issue.
[ January 01, 2008: Message edited by: Arnold Reuser ]
Raghavan Muthu
Ranch Hand

Joined: Apr 20, 2006
Posts: 3344

That looks great. Thank you Arnold

Let's look into it!
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19649
    
  18

Originally posted by Campbell Ritchie:
I don't think you can get the variable name from anything at Runtime.

Local variables not. Class or instance variables can be retrieved using the Class.getDeclaredField method.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37890
    
  22
I obviously wasn't clear. I think he wants to apply a name to a field and alter the field at runtime. I didn't think that was possible, but somebody else has said BCEL might be able to do it.

I don't think it is a reflection question. At least I don't think it is.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: renaming object and creating object in run time
 
Similar Threads
Dynamic type casting
why Noclassdeffounderror
deserialization and casting
Casting Object[] to String[]
data type in java