File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes Parameter of a method - a method Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parameter of a method - a method" Watch "Parameter of a method - a method" New topic

Parameter of a method - a method

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77

Is it possible to have as a parameter of a method a method? Maybe there must be a return typ.

int A (int A()) {}


int B (int C ()) {}

Does anybody know an example?


SCJP 1.4
Srikanth Ramu
Ranch Hand

Joined: Feb 20, 2007
Posts: 76
I guess its not possible to have a method as a parameter. The Parameter of a method should be a datatype.
Remko Strating
Ranch Hand

Joined: Dec 28, 2006
Posts: 893
You could not define a method as a formal parameter, but you can send in a method as an actual parameter. This method should return the parametertype which is expected as formal parametertype in the method.

Here is a working code example

Remko (My website)
SCJP 1.5, SCWCD 1.4, SCDJWS 1.4, SCBCD 1.5, ITIL(Manager), Prince2(Practitioner), Reading/ gaining experience for SCEA,
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 15093

Remko, what your code demonstrates is not passing a method as a parameter to another method. All it does is pass the return value of a method to another method. It's exactly the same as this:

Urs, no, Java does not have "method references" built-in into the language (like function pointers in C or C++). It is possible in Java with reflection. But usually you do not need something like that, it is not good object-oriented design. A better way to do this is to just pass an object reference to the method; the called method can call methods on that object. Through polymorphism you can have different behaviours at runtime.

Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
I agree. Here's the link:
subject: Parameter of a method - a method
It's not a secret anymore!