On java.lang.Class.getMethods()

G

Grisha

Hi all,

What is the reason that getMethods() method (in java.lang.Class)
return only
public methods? After all, couldn't it return all methods, and then,
upon
invocation, throw a SecurityException (or IllegalAccessException)?
What
is the rationale for this?

-----

P.S. After reading http://www.yoda.arachsys.com/java/newsgroups.html,
http://mindprod.com/jgloss/newsgroups.html and
http://www.faqs.org/faqs/computer-lang/java/help/minifaq/, I do feel
this cross-post is justified. If there's a more authoritative source,
please point them out to me. Advocacy was chosen because this is a
point
of Sun's choice, and security -- because this is related to access of
methods.
 
S

Sudsy

Grisha said:
Hi all,

What is the reason that getMethods() method (in java.lang.Class)
return only
public methods? After all, couldn't it return all methods, and then,
upon
invocation, throw a SecurityException (or IllegalAccessException)?
What
is the rationale for this?

Learn how to use javadocs. In this particular case, check out
Class#getDeclaredMethods. Pay attention to the description.
If you're using reflection then the restrictions will manifest
themselves at run-time, just like they're supposed to...
 
J

jarekr

What is the reason that getMethods() method (in java.lang.Class)
return only
public methods? After all, couldn't it return all methods, and then,
upon
invocation, throw a SecurityException (or IllegalAccessException)?
What
is the rationale for this?

Seen getDeclaredMethods()?

There are two functions because:
- getMethods() exhibits all methods that "object" of class implements, (including inherited),
- getDeclaredMethods() exhibits only methods declared in "class" (including private, not including
inherited)

so getDeclaredMethods() is more powerful... but in most cases less convenient than simple getMethods().
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,430
Messages
2,571,676
Members
48,796
Latest member
Greg L.

Latest Threads

Top