Results 1 to 3 of 3
  1. #1
    MarsTeam is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Post Function as argument

    Hello, basically, what I need to do, lets say I have 3 functions:

    Java Code:
    String countNumberOfWords() {... return ...}
    int countDifferential() {... return ...}
    boolean isPartial() {... return ...}
    But let's say, that I need to make it so that every time one of these functions is called, a message System.out.println("One of three function has been called");

    But I don't want to insert that message into implementation of these 3 functions. I want to have a function, lets call it Announcer, which will take as argument one of these 3 functions, and before returning corresponding values(String/int/boolean) will print out that message.
    Or is different approach more efficent ? If yes, how would you do it ? If no, how would you implement my solution ?

    I've tried googling this, but none of results were good for me.
    Thanks for your time.

  2. #2
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,516
    Rep Power
    25

    Default

    I guess you're a C programmer: functions and passing functions as arguments.
    You can pass objects to methods and the object can have callable method.
    The objects can implement an interface defining the method to be called.

    Otherwise you'll have to add println() statements to each method you've listed to say "I was called with ..."

  3. #3
    MarsTeam is offline Member
    Join Date
    Jun 2010
    Posts
    8
    Rep Power
    0

    Default

    Yes I come from heavy c++ background.
    Thanks though, problem is solved now.

    Truly what I needed to do was to make method which would do some checkings before creating method, so in the end it's like this:


    Java Code:
    public interface CheckCallInt<T> {
        T execute();
    }
    .
    .
    .
    Java Code:
    public static <T> T CheckCall(CheckCallInt<T> Call)
        {
            .
            . lots of code to check few things
            .
            return Call.execute();
        }
    .
    .
    .
    Java Code:
     CheckCallInt Call1234 = new CheckCallInt<String>() {
                public String execute() {
                . 
                . do what this method is supposed to do
                .
                };
    then I just input Call1234 into CheckCall, and it first check the method and then executes it.

Similar Threads

  1. Command line argument
    By denisatandi in forum New To Java
    Replies: 8
    Last Post: 10-16-2012, 11:37 PM
  2. Function as argument?
    By martypapa in forum AWT / Swing
    Replies: 2
    Last Post: 02-07-2010, 10:02 AM
  3. Replies: 3
    Last Post: 12-04-2009, 03:08 PM
  4. Method as an argument?
    By StokedOnMe in forum New To Java
    Replies: 13
    Last Post: 09-18-2009, 06:29 AM
  5. Parsing Argument Values
    By vipvan2000 in forum Advanced Java
    Replies: 1
    Last Post: 02-17-2008, 01:41 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •