Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: havig problems with "this " refrence

  1. #1
    Junior Member
    Join Date
    Jan 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default havig problems with "this " refrence

    hi friend i am having problem with " this" refernce when it is passed as an agrument in any method . as everbody knows this alawys point to current live object , i am having few doubts about it . if any body has tutorial which explains is in details then plz provide me the link. a video would like icing on cake.here i am goin to post an j2me code plz help me to understand it

    import javax.microedition.lcdui.*;
    public class demo extends MIDlet implements CommandListener
    {
          Form d1;
          TextField t1,t2;
          Command c1;
          Alert alert;
       public void startApp()
        {
           d1=new Form("Form1");
           t1=new TextField("Enter Your Name","",10,TextField.ANY);
         t2=new TextField("","",20,TextField.ANY);
        Command c1=new Command("OK",Command.SCREEN,0);
        Command c2=new Command("Exit",Command.SCREEN,0);
        alert=new Alert("Field Empty","Text Field is empty",null,AlertType.ERROR);
     
        d1.append(t1);
        d1.append(t2);
        d1.addCommand(c1);
        d1.addCommand(c2);
         d1.setCommandListener(this);
    [COLOR="Red"][SIZE="3"]
    // i want to know about which object to "this"  keyword is poiniting .  is pointing  to "d1"?  is it possible to that "this" to/or can be made point to other object other than "d1" [/SIZE] [/COLOR]
     
        Display.getDisplay(this).setCurrent(d1);
    [COLOR="Red"][SIZE="3"]//is "this" here pointing to DISPLAY OBJECT or any other [/SIZE][/COLOR]
         }
     
       public void pauseApp() 
       {}
       public void destroyApp(boolean unconditional)
      {
           notifyDestroyed();
      }
       public void commandAction(Command c,Displayable d)
        {
                   String lbl1=c.getLabel();
                   String lbl=t1.getString();
     
                   if(lbl.equals(""))
                       {
                          t2.setString("Field Empty");
                        }
                 else
                         {
                          t2.setString("Hello  "+lbl);
                          } 
        }
    }

    thanks
    anoop


  2. #2
    Member DanBrown's Avatar
    Join Date
    Jan 2011
    Posts
    134
    My Mood
    Confused
    Thanks
    1
    Thanked 12 Times in 12 Posts

    Default Re: havig problems with "this " refrence

    d1.append(t1);
    d1.append(t2);
    d1.addCommand(c1);
    d1.addCommand(c2);
    d1.setCommandListener(this);

    // i want to know about which object to "this" keyword is poiniting . is pointing to "d1"? is it possible to that "this" to/or can be made point to other object other than "d1"
    acc to me this keyword will refer to Demo class.


    Tutorials
    Click here
    Thanks and Regards
    Dan Brown

    Common Java Mistakes

  3. #3
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,529
    My Mood
    Hungover
    Thanks
    144
    Thanked 647 Times in 552 Posts

    Default Re: havig problems with "this " refrence

    I'm not really sure what your question is. What are you confused about?

    The "this" keyword points to the Object. When in an inner class, it references the inner Object, but you can use the outer Object's this keyword by using the "OuterClassName.this" keyword.

    What did google tell you? After googling "java this", I found this (no pun intended) tutorial: Using the this Keyword (The Java™ Tutorials > Learning the Java Language > Classes and Objects)
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

Similar Threads

  1. Replies: 6
    Last Post: November 12th, 2010, 04:40 AM
  2. Java says:"Hello World". I say:"It works!"
    By Davidovic in forum Member Introductions
    Replies: 4
    Last Post: June 29th, 2010, 08:13 AM
  3. Replies: 1
    Last Post: March 31st, 2010, 10:42 PM
  4. "java.lang.NoSuchMethodError: main" and "fatal exception occured."
    By joachim89 in forum What's Wrong With My Code?
    Replies: 2
    Last Post: January 10th, 2010, 08:35 AM
  5. Replies: 4
    Last Post: August 13th, 2009, 06:54 AM