• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

this in java

 
Ranch Hand
Posts: 92
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Sheriff
Posts: 22848
132
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Greenhorn
Posts: 8
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
author
Posts: 23959
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Henry and Rob....I got the point...
 
reply
    Bookmark Topic Watch Topic
  • New Topic