Results 1 to 11 of 11
  1. #1
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Question [SOLVED] I want to Invoke an Instance Method as a Parameter is it Possible?

    Hello EveryOne :)

    I have an idea to solve a problem but i am not sur if it's Viable or not, and if it is,
    I would like to know the right java syntax for it.

    The Idea:

    Invoke an Instance Method from another class to be a Parameter of an Instance Method.


    The Example i am working on:

    Java Code:
    // Snapshot of The First Class;
    // I want to invoke MyFirstEventExample() Method from a second class
    // to the actionPerformed() Method as a parameter;
    
        public void actionPerformed(ActionEvent MyFirstEventExample())
        {
            this.Output.append("Action occurred in the Button Handler: " +"\n");
            this.Output.append("Hello I am Laythe :D " +"\n");
            this.Output.append(MyFirstEventExample() +"\n");
        }
    Java Code:
    // Snapshot of The Second Class;
    // There is other classes are involved in the projecte, but i am not conserned 
    // about them at the moment;
    
        public void MyFirstEventExample()
        {
            // Create an event source;
            MyFirstEventSource S1 = new MyFirstEventSource();
    
            // Register an event listener to the event source;
            // the Method addMyEventListener is invoked from MyFirstEventSource class;
            S1.addMyEventListener( new MyFirstEventListenerImpl() );
    
            // Triger an event;
            // the Method TriggerSomethingEvent is alsow invoked from
            // MyFirstEventSource class;
            S1.TriggerSomethingEvent("I won the lottery!");
    
            // Trigger another event
            S1.TriggerSomethingEvent("She said Yes!");
        }

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    You can't pass methods as parameters, but you can use Interface parameters and so get the same effect. This is how much of Swing works.

  3. #3
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Thank you Sir Fubarable for your quick answer :)

    I will try to solve the problem using the Interface parameters and i will let you know if i succeed.

    Peace :)

  4. #4
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    You know i am still Learning Java so i did a research about using Interface parameters but i got to nowhere.
    would you point me to the right direction to learn about using Interface parameters.

    Thanks in advance :)

  5. #5
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Do you understand Java interfaces in general? If not, please read up on this here: Lesson: Interfaces and Inheritance (The Java™ Tutorials > Learning the Java Language)

    Then Google on ActionListener and Runnable interfaces to see how they work. They essentially work by doing what you are trying to do.
    Last edited by Fubarable; 06-10-2009 at 10:22 PM.

  6. #6
    Singing Boyo is offline Senior Member
    Join Date
    Mar 2009
    Posts
    552
    Rep Power
    6

    Default

    Note about java conventions for method names: They begin with lowercase letters.

    You seem to be somewhat confused, and I'm not sure how well interfaces would work for solving your problem. What you may want is a way to call the method from the actionPerformed method.(you could probably just make the method you want to call static) Then you could just call it.

    e.g.
    Java Code:
    public class StringGenerator{
         public String generateString(){
              return "A generated string";
         }
    }
    Java Code:
    public class GeneratorImplementation{
         public static void main(String[] args){
              System.out.println(StringGenerator.generateString);
         }
    }
    Output: (or it should be... wrote this without a compiler)
    Java Code:
    A generated string
    Last edited by Singing Boyo; 06-10-2009 at 11:08 PM.
    If the above doesn't make sense to you, ignore it, but remember it - might be useful!
    And if you just randomly taught yourself to program, well... you're just like me!

  7. #7
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Hello.

    I am studing the Java Interfaces to see if they may help me to solve the problem, thank you Sir Fubarable for the link.

    Sir Singing Boyo, I konw about the java conventions for method names (They begin with lowercase letters), I just like to write the first letter of the Methods I create with uppercase to separate My Methods from the java's native methods.

    I know about the example you gived me, it just dosen't solve the problem, with my modeste knowledge i think i need to call a Method within another Method, but it seems not possible as sir Fubarable mentioned so i am tring to figurout away to do it with Interfaces.

    If anyone have an Idea Please share it with us ;)
    Last edited by Laythe; 06-11-2009 at 03:37 PM.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I may have a better idea of what you're trying to do if you could post some compilable and perhaps runnable code that illustrates the current problem.

  9. #9
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    Hello.

    Thank you Sir Fubarable for your interest in help me :)

    I was doing two exercices about "Event Handling", so i thought is it possible to Merge this two exercices?
    I tried to find a way for this couple days but maybe cause i am at the beginning of the road i didn't succeed yet.

    this is the codes of the two exercices:

    Java Code:
    Sir Fubarable Take a Look at your Private Message

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    I've looked at your two examples, and in fact they both use Interfaces as I suggested above.

    In your first example, you are creating event listeners, placing them in a list and then triggering them at some event.

    Do you understand that this is how Swing works with JButtons. The addActionListener method of JButton's expects an ActionListener interface-implementing object. It then stores this object in an internal list, and when the button is pressed, it iterates through all the ActionListener objects stored in its list, calling the actionPerformed method of each of them. So in a sense, your second program, the Swing program already does what your first program does, except much of the inner workings are done behind the scenes.

    Edit: for anyone else wanting to see the full program code he emailed to me, I'm including two jar files that I've changed extension on to zip for downloading purposes. The source code is in there. I made one small change to display output in joptionpanes if console is not available.
    Attached Files Attached Files
    Last edited by Fubarable; 06-12-2009 at 05:23 PM.

  11. #11
    Laythe's Avatar
    Laythe is offline Member
    Join Date
    Mar 2009
    Posts
    28
    Rep Power
    0

    Default

    I got it know thank you Sir Fubarable :)

    I had that Idea cos i didn't understood the code well as i do now.

    but i still think invoking a Method within another Method could be a useful Idea, remeber it's just a novice thought :p

Similar Threads

  1. Can a method take itself as parameter?
    By bukake in forum New To Java
    Replies: 10
    Last Post: 09-06-2008, 10:26 PM
  2. Input parameter of Main method
    By Java Tip in forum Java Tip
    Replies: 1
    Last Post: 07-12-2008, 07:24 PM
  3. public instance method
    By steve123 in forum New To Java
    Replies: 5
    Last Post: 06-20-2008, 09:45 PM
  4. Instantiation using an instance factory method
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 03-29-2008, 01:35 PM
  5. Instantiation using an instance factory method
    By JavaBean in forum Java Tip
    Replies: 0
    Last Post: 09-26-2007, 09:25 PM

Posting Permissions

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