• 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Tim Cooke
Sheriffs:
  • Rob Spoor
  • Liutauras Vilda
  • paul wheaton
Saloon Keepers:
  • Tim Holloway
  • Tim Moores
  • Mikalai Zaikin
  • Carey Brown
  • Piet Souris
Bartenders:
  • Stephan van Hulst

dynamic access of interface members

 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
using java.lang.reflect.Fields we can access the Class variables dynamically
But i want to access the members of Interface dynamically

give the correct Solution
 
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by vigneswaran sathischandrababu:
using java.lang.reflect.Fields we can access the Class variables dynamically
But i want to access the members of Interface dynamically

give the correct Solution



Why do you want to do this? Interfaces typically only have public static constants declared in them as members and you don't need Reflections to find them.

Cheers,
Martijn
 
Vigneswaran Sathischandrababu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
because i want to load some members of Interface into Map and some other members into another Map

so i need ........

is there any solution ?
 
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do the same, as you do for class. java.lang.Class is used to represent classes and interfaces, both.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Martijn Verburg:
Interfaces typically only have public static constants declared in them as members



Fields in interfaces are not, typically, static, they are final.
[ August 27, 2008: Message edited by: Adeel Ansari ]
 
Vigneswaran Sathischandrababu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i can't do the same
because for Class, field.get(object) is the method of accessing members

but for Interface how can i create the Object....

Is there any way... ?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In fact, interfaces are there to implement. Why you need to go reflection for interfaces? You didn't really explain. The reason you have given can not be taken as justification, I believe. One can do it without reflection. Where is the problem?
 
Vigneswaran Sathischandrababu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Actually i will put lot of variables in the Interface.
Dynamically i want to load some variables into one Map depends on one Input value. If i got anyoher input i want to load some other variables into Map.

those variables are hardcoded values.

If i manually load variables from interface to Map then the lines of code is high when number of variable is high.

( Note : Actually i will hardcode some large values. These values i want in Map and also these will act as Key in some other places. )
 
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vignesh:
i can't do the same
because for Class, field.get(object) is the method of accessing members

but for Interface how can i create the Object....

Is there any way... ?



How are you creating the object when you do this with a class ? If you show us the code you are using to do this, we may be able to understand your problem better. At the moment it is not making much sense.
 
Java Cowboy
Posts: 16084
88
Android Scala IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Vignesh, please check your private messages. You can see them by clicking My Private Messages.
 
Vigneswaran Sathischandrababu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Class Test{
String secOne = "somevalue";
String secTwo = "somevalue";
String secThree = "somevalue";

String valOne = "somevalue";
String valOne = "somevalue";
String valOne = "somevalue";
}
like this i have class.

Depends on some input i need to load the values into map that variables starting with "sec" or "val".

Field[] field = Test.class.getFields();
Test testObj = new Test();
for(int i=0;i<field.length;i++){
system.out.println( field[i].getName+" - "+field[i].get(testObj));
}

Using the above code i can access the class members dynamically
Like this Is there anything for access Interface members ?
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Vignesh:
Actually i will put lot of variables in the Interface.
Dynamically i want to load some variables into one Map depends on one Input value. If i got anyoher input i want to load some other variables into Map.



Why not have a map in the interface, I mean variable name as a key and value as pair. Why go reflection. I hope you are getting me. We should avoid this kinda stuff. Use reflection where its necessary, or giving you a real benefit over norm.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Isn't it fine?
 
Vigneswaran Sathischandrababu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I need without implementing that Interface into any class.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This might be a good time to point out that using interfaces primarily to keep constants is considered bad design.
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adeel Ansari:


Fields in interfaces are not, typically, static, they are final.

[ August 27, 2008: Message edited by: Adeel Ansari ]



Oops, right you are, must switch brain on before typing
 
Joanne Neal
Rancher
Posts: 3742
16
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Try passing null to the get() method.
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Adeel Ansari:


Fields in interfaces are not, typically, static, they are final.



Quite wrong, I'm afraid. Fields in interfaces are always implicitly both static and final, whether you declare them so or not.

The Field.get() method will happily accept null as a parameter if you are fetching the value of a static field (this is stated explicitly in the Javadoc.)

So Vigneswaran Sathischandrababu, you can use the same code for an interface that you're using for other classes, except that instead of "testObj", just use "null".
 
Martijn Verburg
author
Posts: 3285
13
Mac OS X Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:


Quite wrong, I'm afraid. Fields in interfaces are always implicitly both static and final, whether you declare them so or not.



<Blinks> <Races off to look at his Java book collection featuring cute animals>......

Cripes, I had totally missed that. Once more I am humbled on a forum site, back to basics for this so called 'experienced' Java Dev
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Ernest Friedman-Hill:
Quite wrong, I'm afraid. Fields in interfaces are always implicitly both static and final, whether you declare them so or not.



Really! I just remember the final thingy. Anyhow, this made the bad design thing quite obvious.
 
Vigneswaran Sathischandrababu
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If i pass null to get() then i got it...

Thanks a lot
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Why do you put those values into an interface? Sounds to me like two property files might be better places to put those values.
 
Adeel Ansari
Ranch Hand
Posts: 2874
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Martijn Verburg:
Cripes, I had totally missed that. Once more I am humbled on a forum site, back to basics for this so called 'experienced' Java Dev



Cent percent agreement.
Thanks Ernest for pointing it out, now I will remember this for sure.
 
Creativity is allowing yourself to make mistakes; art is knowing which ones to keep. Keep this tiny ad:
Smokeless wood heat with a rocket mass heater
https://woodheat.net
reply
    Bookmark Topic Watch Topic
  • New Topic