Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

this in java

 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello,
I have a concern as follows:
Consider 2 classes:

public class Main {

public Main() {
new abc(this); // Line 1
new abc(new Main()); // Line 2
}

public static void main(String[] args) {

}
}

class abc
{
Main m;

abc(Main m)
{
this.m=m;
}
}


so passing the this reference as in line 1 is equivalent to constructing an object of the Main class.
In short this when passed,internally creates object of the class..is this conclusion correct...

help appreciated
cheers
amal shah
 
Rob Spoor
Sheriff
Pie
Posts: 20608
63
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by amal shah:
so passing the this reference as in line 1 is equivalent to constructing an object of the Main class.
In short this when passed,internally creates object of the class..is this conclusion correct...

Not quite correct.

When you use "this", you are NOT creating a new object, it uses the object that is already created.

Actually, you're code will never run properly, as you'll get a StackOverflowError. What happens is the following:
1) You create an instance of Main manually.
2) The Main() constructor runs.
3) The Main() constructor creates another instance of Main on // line 2
4) Go to step 2.
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you use "this", you are NOT creating a new object, it uses the object that is already created.


which object does it use......there can n number of objects for a class

Actually, you're code will never run properly, as you'll get a StackOverflowError.


I don't think so....it ran properly.....it is just a parameter passed..

I am sort of confused with (this) concept....
pls help

cheers
amal shah
 
Vaibhav Aggarwal
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Check out the below code The ouput for above code will be.


You will see that when we use "this" then we are refering to the current instance of the class. So while the display method changed the value of the local variable, updateInstanceVariable method updated the instance member of the current object.
[ September 24, 2007: Message edited by: Vaibhav Aggarwal ]
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will see that when we use "this" then we are refering to the current instance of the class. So while the display method changed the value of the local variable, updateInstanceVariable method updated the instance member of the current object.


Your code above displays use of (this) when the local variable hides the instance member of the class....

But i am confused with the code that i pasted in my first post with respect to usage of (this) passed in as parameter....

please help me understand this concept...

help appreciated
cheers
amal shah
 
Henry Wong
author
Marshal
Pie
Posts: 21412
84
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think so....it ran properly.....it is just a parameter passed..


Amal,

When Rob is saying that your problem "will never run properly", he meant that you can't actually instantiate a Main object without an runtime exception. The reason your program doesn't generate a runtime exception, is because you never intantiate a Main object -- which means that you never tested any of the constructors that you are trying to figure out.

Henry
 
amal shah
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Henry and Rob....I got the point...
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic