Meaningless Drivel is fun!*
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


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Parameter of a method - a method" Watch "Parameter of a method - a method" New topic
Author

Parameter of a method - a method

Urs Waefler
Ranch Hand

Joined: Mar 13, 2007
Posts: 77
Hi

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

int A (int A()) {}

OR

int B (int C ()) {}

Does anybody know an example?

Regards
Urs


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: 14420
    
  23

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
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Parameter of a method - a method