• 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

Handling No-reflexion constructed objets with reflexion-api

 
Greenhorn
Posts: 7
Netscape Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I have an object that has been already constructed using non-reflexive API, but I now I new to access to some of it methods and fields using reflexive API.
But for doing that I must have a object Class referencing such Original object.
Let say I have and Object obj, wich I created before using Object obj = new Object; but now I need to access it using reflixive API, so how can I have a object Class that refers to such object obj.
Any help please!!
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fabricio Zelada wrote:I have an object that has been already constructed using non-reflexive API, but I now I new to access to some of it methods and fields using reflexive API.
But for doing that I must have a object Class referencing such Original object.
Let say I have and Object obj, wich I created before using Object obj = new Object; but now I need to access it using reflixive API, so how can I have a object Class that refers to such object obj.
Any help please!!



Well, you can start with....



Henry
 
Marshal
Posts: 79180
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume you have seen the reflection part of the Java Tutorials?
 
Fabricio Zelada
Greenhorn
Posts: 7
Netscape Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks for the welcome and for the answer, but the problem is that I have the object obj just as string, this is because I have so many object like this been created dinamicaly. Any thought?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fabricio Zelada wrote:Thanks for the welcome and for the answer, but the problem is that I have the object obj just as string, this is because I have so many object like this been created dinamicaly. Any thought?




What do you mean by "obj just as string"? A string object is just like any other object, and hence, you can get the Class class for the string object. Or do you mean that the string object is holding the name of the class that you want? In that case, the forName() method of the Class class, can get you the Class instance that you want.

Regardless, please take a look at the tutorial that Campbell posted again. The forName() method was one of the early examples provided.

Henry

 
Fabricio Zelada
Greenhorn
Posts: 7
Netscape Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah right, I think I was not clear, in the begging I create object obj by using by common method (by calling the constructor).
But later I need to use its methods. The only reference of such object, its name, is contained into a String.

So I cannot use forName(String) method, because I will have to create other object. I would like to create a Class object that reference object obj. But only thing I have is its name inside the String. So is there any way?
 
Henry Wong
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Fabricio Zelada wrote:Yeah right, I think I was not clear, in the begging I create object obj by using by common method (by calling the constructor).
But later I need to use its methods. The only reference of such object, its name, is contained into a String.

So I cannot use forName(String) method, because I will have to create other object. I would like to create a Class object that reference object obj. But only thing I have is its name inside the String. So is there any way?




The forName(String) method, only creates a Class object if it doesn't exist. And it doesn't create an instance of the object -- you have to use the Class object for that.


And BTW, you are completely not clear. Either you have a reference to the object or you don't. If you do, then use getClass(). If you don't, and have the name, then use "forName()". Or get a reference.

Henry
reply
    Bookmark Topic Watch Topic
  • New Topic