This week's book giveaway is in the OCMJEA forum.
We're giving away four copies of OCM Java EE 6 Enterprise Architect Exam Guide and have Paul Allen & Joseph Bambara on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes this in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "this in java" Watch "this in java" New topic
Author

this in java

amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
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

Joined: Oct 27, 2005
Posts: 19684
    
  20

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.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
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

Joined: Sep 12, 2007
Posts: 8
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

Joined: May 05, 2006
Posts: 92
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
Sheriff

Joined: Sep 28, 2004
Posts: 18765
    
  40

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


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
amal shah
Ranch Hand

Joined: May 05, 2006
Posts: 92
Thanks Henry and Rob....I got the point...
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: this in java