File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Can we access static members via reflection?

 
Faisal syed
Ranch Hand
Posts: 35
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Friends,
Can we access static members via reflection?
 
Henry Wong
author
Marshal
Pie
Posts: 20834
75
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Faisal syed wrote:Hello Friends,
Can we access static members via reflection?



yes .... you can access static fields of a class.
 
Mike Simmons
Ranch Hand
Posts: 3028
10
  • 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And more generally, you can access any static members, not just fields.
 
Winston Gutkowski
Bartender
Pie
Posts: 9475
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote: Do you want to?

some time, it is useful.. for instance, annotations.
 
Winston Gutkowski
Bartender
Pie
Posts: 9475
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 5575
Eclipse IDE Java Windows XP
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 9475
50
Eclipse IDE Hibernate Ubuntu
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic