Closed Thread
Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: about this keyword ..~

  1. #1
    R3.RyozKidz Guest

    about this keyword ..~

    i need some link about this reference keyword...~
    i wan to know other uses of this reference ( i already know how to use this reference on instance variable but what i nid to know is usage of this reference on constructor and method ) ..~ i need it urgently ....~!!!!

  2. CODECALL Circuit advertisement

     
  3. #2
    Join Date
    May 2009
    Location
    Belgium
    Posts
    1,879
    Rep Power
    24

    Re: about this keyword ..~

    In a constructor you use it when you use the same name for the parameter as for the declaration
    [highlight=Java]
    public class Myclass{
    private String test

    public Myclass(String test){
    this.test = test;
    }
    [/highlight]
    As you in the constructor you give the parameter 'test', but 'test' allready exists as class declaration. So you need to make clear to Java which test you mean. By using 'this' you say it's the one above. Without this it will always use the local one(meaning, the one between { and } )

  4. #3
    Sinipull's Avatar
    Sinipull is offline Programming Expert
    Join Date
    Jun 2009
    Location
    Tallinn, Estonia, Estonia
    Posts
    382
    Rep Power
    13

    Re: about this keyword ..~

    "this" as a constructor can be used to call other constructors in the same class, so you don't have to write a lot of duplicating code.
    Like this:

    Code:
    class Car{	
    	public Car(){
    		this(50);
    	}
    	
    	public Car(int horsePower){
    		// the code
    	}
    }
    whenever "new Car()" is called, horsePower is automatically set to 50.
    however, user has an opportunity to set horsePower manually by calling "new Car( int )"

  5. #4
    R3.RyozKidz Guest

    Re: about this keyword ..~

    what if there are more than 1 constructor , then which constructor will java call..?

    1) this.methodName( ... ) <- this "this" is used as ..?
    2) methodName( this ) <- then "this" is used as ..?

  6. #5
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: about this keyword ..~

    Quote Originally Posted by R3.RyozKidz
    what if there are more than 1 constructor , then which constructor will java call..?
    What do you mean? It depends on which parameters are passed during object construction.
    [highlight=Java]public class SomeClass
    {
    public SomeClass()
    {
    System.out.println("Empty Constructor");
    }

    public SomeClass(Object o)
    {
    System.out.println("Object Constructor");
    System.out.println(o);
    }

    public String toString()
    {
    return "I'm the passed object!";
    }

    public static void main(String[] args)
    {
    new SomeClass(new SomeClass());
    }
    }[/highlight]

    Quote Originally Posted by R3.RyozKidz
    1) this.methodName( ... ) <- this "this" is used as ..?
    2) methodName( this ) <- then "this" is used as ..?
    In the first case, this is being used as an object to be dereferenced. The this keyword always points to the object that the method is located in, nothing more. It would be as if each method was passed an implicit parameter called "this" that has the address value of the object the method is actually in, and in fact this is precisely how it works. So when you say "this.methodName()" it's exactly the same as saying "methodName()" with no object in front of it, assuming such a method existed in the object, but could be considered clearer.

    In the second case, you're calling methodName() and passing this as a parameter. Doing so is almost completely pointless unless it's a method that requires and modifies an object of the same type as the object the method is in OR one of the parent classes of the object the method is in.
    Wow I changed my sig!

  7. #6
    R3.RyozKidz Guest

    Re: about this keyword ..~

    Quote Originally Posted by ZekeDragon View Post
    In the first case, this is being used as an object to be dereferenced. The this keyword always points to the object that the method is located in, nothing more. It would be as if each method was passed an implicit parameter called "this" that has the address value of the object the method is actually in, and in fact this is precisely how it works. So when you say "this.methodName()" it's exactly the same as saying "methodName()" with no object in front of it, assuming such a method existed in the object, but could be considered clearer.
    public class ABC
    {
    public static void main(String args[])
    {
    XYZ object1 = new XYZ();
    JKL object2 = new JKL();

    this.methodName();

    }
    }

    Case 1 : If only one object contains the methodName , so the method will be execute .
    Case 2 : If both of the object contain the same methodName , so how java decides which 1 will going to execute ..?

  8. #7
    R3.RyozKidz Guest

    Re: about this keyword ..~

    Quote Originally Posted by ZekeDragon View Post
    In the second case, you're calling methodName() and passing this as a parameter. Doing so is almost completely pointless unless it's a method that requires and modifies an object of the same type as the object the method is in OR one of the parent classes of the object the method is in.
    let say the method is declared as so :
    public void methodName(int x)

    calling the method:
    methodName(this);

    will the java automatically passing the correct argument to the method ..?
    is there a necessary the argument must be declared as an instance variable ..?

  9. #8
    Join Date
    Sep 2007
    Location
    Karlstad, Sweden
    Posts
    3,082
    Blog Entries
    7
    Rep Power
    42

    Re: about this keyword ..~

    ok, think like this:
    Code:
    class circle {
      int x, y, r;
      public int area (int r) {
         // here now, we have two r variables, the method parameter, and the class member variable. to access the parameter, we do this:
         int diameter = r * 2;
         // but, maybe the class member r needs to be used in some case...
         int memberDiameter = this.r * 2;
         // now, I use this to tell that it's the member variable I'm using, not the method variable.
        }
    
        // but this can also be used to separate which method to use
        public int test() {
            this.area(7);
        }
    
        // because, if this was an inherited class, you could have an method in parent
    
       public int test2 () {
           super.area(89);
       }
        // now super specifies it's the parent class version to use, not the one in THIS class.
    }
    well, this can't compile or anything, but I hope it can demonstrate a little bit more on how this can be used.
    __________________________________________
    I study Information Systems at Karlstad University when I'm not on CodeCall

  10. #9
    Join Date
    Jul 2009
    Location
    Santa Clarita, CA
    Posts
    2,111
    Blog Entries
    47
    Rep Power
    31

    Re: about this keyword ..~

    Quote Originally Posted by R3.RyozKidz
    Code:
    public class ABC
    {
        public static void main(String args[])
        {
            XYZ object1 = new XYZ();
            JKL object2 = new JKL();
    
            this.methodName();
        }
    }
    Case 1 : If only one object contains the methodName , so the method will be execute .
    Case 2 : If both of the object contain the same methodName , so how java decides which 1 will going to execute ..?
    Neither of those cases would be the problem, in fact this code will not work at all. First off, the this keyword does not work at all in static functions, you can only use this inside of non-static methods. This is because in a static method you may not have an object instantiated. The this keyword only works on the current instantiated object.

    Next, even if it were done in an instantiated object, the this keyword would be acting on the currently instantiated ABC object, not XYZ or JKL. It would completely ignore those objects, even if they both had methods called methodName().

    Consider this code:
    [highlight=Java]public class ABC
    {
    public ABC(int x)
    {
    this.num = x;
    }

    public void methodName()
    {
    System.out.println("Number " + this.num);
    }

    public void thisCheck()
    {
    this.methodName();
    }

    public static void main(String[] args)
    {
    ABC one = new ABC(1);
    ABC two = new ABC(2);
    one.methodName();
    two.thisCheck();
    }

    private int num;
    }[/highlight]
    This code shows how the this keyword works both with methods and properties.

    When you pass this, you pass the object that the method is running in itself. A useful method for this is println:
    [highlight=Java]public class ABC
    {
    public String toString()
    {
    return "This is the object itself.";
    }

    public void aFunction()
    {
    System.out.println(this);
    }

    public static void main(String[] args)
    {
    (new ABC()).aFunction();
    }
    }[/highlight]
    And there you have it.
    Wow I changed my sig!

  11. #10
    R3.RyozKidz Guest

    Re: about this keyword ..~

    Quote Originally Posted by ZekeDragon View Post
    When you pass this, you pass the object that the method is running in itself. A useful method for this is println:
    [highlight=Java]public class ABC
    {
    public String toString()
    {
    return "This is the object itself.";
    }

    public void aFunction()
    {
    System.out.println(this);
    }

    public static void main(String[] args)
    {
    (new ABC()).aFunction();
    }
    }[/highlight]
    And there you have it.
    Only just this part i cant understand which is pass the object that the method is running in itself ..! what i know is the result is "This is the object itself"
    The "this" is the aFunction is trying to pass the whole object of class ABC into it ...?

    Code:
    public class ABC
    {
        public String printMessage()
        {
            return "Hallo";
        }
        
        public String displayMessage()
        {
            return "Hallo1";
        }
        
        public void aFunction()
        {
            System.out.println(this);
        }
        
        public static void main(String args[])
        {
            (new ABC()).aFunction();
        }
    }
    in this case , what will it print?

Closed Thread
Page 1 of 2 12 LastLast

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Which keyword tool do you think that it is best?
    By stephen186 in forum Search Engine Optimization
    Replies: 10
    Last Post: 12-29-2011, 01:47 AM
  2. Keyword in URL?
    By stephen186 in forum Search Engine Optimization
    Replies: 13
    Last Post: 12-22-2010, 02:47 AM
  3. use of attribute keyword in C
    By onus in forum C and C++
    Replies: 13
    Last Post: 11-22-2010, 12:17 PM
  4. keyword advisor
    By marta in forum Search Engine Optimization
    Replies: 5
    Last Post: 11-27-2007, 07:49 PM
  5. How do I see what I rank for a certain keyword?
    By Kaabi in forum Search Engine Optimization
    Replies: 12
    Last Post: 01-02-2007, 11:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

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