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 ....~!!!!![]()
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 } )
"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:
whenever "new Car()" is called, horsePower is automatically set to 50.Code:class Car{ public Car(){ this(50); } public Car(int horsePower){ // the code } }
however, user has an opportunity to set horsePower manually by calling "new Car( int )"
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 ..?
What do you mean? It depends on which parameters are passed during object construction.Originally Posted by R3.RyozKidz
[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]
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.Originally Posted by R3.RyozKidz
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!
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 ..?
ok, think like this:
well, this can't compile or anything, but I hope it can demonstrate a little bit more on how this can be used.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. }
__________________________________________
I study Information Systems at Karlstad University when I'm not on CodeCall
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.Originally Posted by R3.RyozKidz
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!
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 ...?
in this case , what will it print?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(); } }
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks