• Post Reply Bookmark Topic Watch Topic
  • New Topic

Fields in methods using Reflection

 
Rohith Pavithran
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To get its fields from a class i used
Class classload=Class.forName("<Class name>");
Field []fieldspresent=classload.getDeclaredFields();

what should be done to access fields (local to a method in the class)
The methods from the class is obtained as
Method []methodspresent=classload.getDeclaredmethods();
 
Ulf Dittmer
Rancher
Posts: 42970
73
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Those fields can not be accessed through reflection. They exist (and have meaning) only during execution of the method, so there's no point in accessing them outside of a method call.
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by Ulf Dittmer:
Those fields can not be accessed through reflection.


It might clarify things to say that they're not "fields" at all -- they're "local variables."
 
Emanuel Ramos
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi, I have a similar question:

Using the function getParameterTypes(), it is possible to find all parameter types from a Method. However, is there a way to find all parameters NAMES of a Method?

Thanks
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24213
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

Welcome to JavaRanch!

No, not using reflection. The information may be in the class file, as part of the debugging information, if present. You'd have to parse the class file directly, or use a library that does so, to get at this information.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!