Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

renaming object and creating object in run time

 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ?
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79535
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79535
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 196
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That looks great. Thank you Arnold

Let's look into it!
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79535
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
The only taste of success some people get is to take a bite out of you. Or this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic