aspose file tools*
The moose likes Java in General and the fly likes Can we access static members via reflection? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "Can we access static members via reflection?" Watch "Can we access static members via reflection?" New topic
Author

Can we access static members via reflection?

Faisal syed
Ranch Hand

Joined: Mar 25, 2011
Posts: 35
Hello Friends,
Can we access static members via reflection?
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18707
    
  40

Faisal syed wrote:Hello Friends,
Can we access static members via reflection?



yes .... you can access static fields of a class.


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 3007
    
    9
And more generally, you can access any static members, not just fields.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7639
    
  19

Faisal syed wrote:Can we access static members via reflection?

You can do pretty much anything you like with reflection. The question is: Do you want to? And by that I mean: Is it wise?

My experience: 99% of the time, it isn't.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Winston Gutkowski wrote: Do you want to?

some time, it is useful.. for instance, annotations.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7639
    
  19

Seetharaman Venkatasamy wrote:some time, it is useful.. for instance, annotations.

OK, but how often do you write an annotations processor? My hope would be once.

Reflection is an aberration in Java; and if it isn't, you need to ask yourself two questions:
1. Do I need to do this?
2. Is Java the right language for doing this?

Winston
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Winston Gutkowski wrote:
2. Is Java the right language for doing this?

right now, I cant answer to this question. because I know only one language some what- JAVA
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7639
    
  19

Seetharaman Venkatasamy wrote:right now, I cant answer to this question. because I know only one language some what- JAVA

Oh well, plenty of time. You might want to look at a scripting language, or something like Ruby or Python, because they have way more scope for doing the sort of stuff that you need reflection for in Java. It doesn't mean they're necessarily better; just different. It boils down to picking the right tool for the job.

HIH

Winston
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Can we access static members via reflection?