• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

renaming object and creating object in run time

 
krishna balaji
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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"?
 
krishna balaji
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 196
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 3381
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That looks great. Thank you Arnold

Let's look into it!
 
Rob Spoor
Sheriff
Pie
Posts: 20611
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49827
69
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic