Results 1 to 6 of 6
  1. #1
    Webstorms is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Passing "this" as a parameter

    Hey, I was wondering what "this" referes to, in the context of a method or conseg. dosomething(this); What does this represent -- what data?
    Please clear me up and give me an example if possibly. Thanks. :)

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

    Default Re: Passing "this" as a parameter

    This is simply the current object that you are in. So if the code is being called from within your Fubar class, then it is the current Fubar object that is calling the code.

  3. #3
    Webstorms is offline Member
    Join Date
    Sep 2011
    Posts
    5
    Rep Power
    0

    Default Re: Passing "this" as a parameter

    OK, starting to make sense yet difficult to get my head around. Could you give me a possible example?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

    Default Re: Passing "this" as a parameter

    The classic example is if the class also implements an interface and you need to pass the current instance into a method that requires the interface. For instance if a Swing GUI class implements the ActionListener interface and you need to add the current instance as the ActionListener for your GUI's buttons: ActionListener Tutorial

  5. #5
    Cosmos is offline Member
    Join Date
    Jul 2011
    Location
    New Delhi,India
    Posts
    56
    Rep Power
    0

    Default Re: Passing "this" as a parameter

    let me explain you thoroughly....

    1st without using the "this"-

    Java Code:
        import java.awt.*;
        import java.awt.event.*;
        import java.applet.*;
    
        class MyButton implements ActionListener{
    
        MyButton(){
        }
    
        public void actionPerformed(ActionEvent ae){
        System.out.println("Button is clicked");
        }
    
        }
    
        public class MyApplet extends Applet{
    
        Button b;
    
        public void init(){
        b=new Button("Click");
        MyButton mb=new MyButton();
        b.addActionListener(mb);
        }
    
    
        }
    in this case,the event source,i.e. the button is in MyApplet class and it is handled in MyButton class.....so we pass the object of MyButton class to the argument of addActionListener method which specify that the button event will be handled in the class MyButton(and not in the class MyApplet)....
    we have implemented ActionListener in MyButton class and not in the MyApplet class,so we have to give the implementation of actionPerformed() method in the class MyButton....but as the program execution starts with MyApplet class,and event will be generated in MyApplet class itself(which is presently not implementing ActionListener),therefore we have to specify in addActionListener method as the name of the class that will be handling the ActionEvent generated by the Button b in MyApplet class,as in our example the MyButton class is handling the action event,so we specify the object of MyButton class in addActionListener method

    --------------------------

    in 2nd implementation(i.e.using "this")

    Java Code:
        import java.awt.*;
        import java.awt.event.*;
        import java.applet.*;
    
        public class MyApplet extends Applet implements ActionListener{
    
        Button b;
    
        public void init(){
        b=new Button("Click");
        b.addActionListener(this);
        }
    
        public void actionPerformed(ActionEvent ae){
        System.out.println("button is clicked");
        }
    
        }
    in this implementation,we implemented "ActionListener" in the same class that contains the button object i.e.we implemented the ActionListener in the same class that will generate the event.....as we have implemented the ActionListener in the class MyApplet itself,we have to tell the compiler that button will be handled in the same class that contains the Button,therefore we specified "this" in the argument list of addActionListener method,which tells the compiler that the action event generated by the button will be handled in "this" class,i.e. the same class that contains the button...
    Last edited by Fubarable; 11-12-2011 at 06:44 PM. Reason: code tags added

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    25

Similar Threads

  1. "invalid command line parameter" error
    By MCinPgh in forum Eclipse
    Replies: 1
    Last Post: 09-09-2011, 09:03 PM
  2. Replies: 3
    Last Post: 10-12-2010, 04:21 PM
  3. Replies: 1
    Last Post: 01-21-2010, 09:20 AM
  4. Replies: 2
    Last Post: 01-24-2009, 06:56 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

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