Meaningless Drivel is fun!*
The moose likes Beginning Java and the fly likes problem in when to use this in java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "problem in when to use this in java" Watch "problem in when to use this in java" New topic
Author

problem in when to use this in java

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
hello everyone, i have a question, it can be a silly question but i need to clear this:
i have a constructor and i m using this keyword in in the Timer class, and as i know this keyword refer to the current object, but if i replace this to my constructor name, it's an error, my question is how it's refer to the current object, and can i use something else here instead of this, below is my constructor..

Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
You called your constructor from somewhere else, using something like:

Board someName = new Board();

So, when you enter the constructor to create the someName Board object, the keyword 'this' IN THIS SPECIFIC CASE refers to the object someName. The keyword 'this' relieves you of having to know the name or instance of the object being acted on.

Now that we have that clear (I hope) and recognize that the 'this' keyword is quite handy, why would you want to substitute it with something else? What would that something else be?


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
i don't want to substitute it with something else, but actually i asked this question to clear use of 'this' keyword.
and the first line of the code is commented, please ignore it, i am calling the constructor from some other class like this:

Board b = new Board();

and as you said at this time my this keyword refers to the b object of class Board, isn't it??
and if i use 'b' instead of this, is that right??
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
this time my this keyword refers to the b object of class Board, isn't it??


Yes. That you have right.

and if i use 'b' instead of this, is that right??


No, that's not right. First, consider the compiler's problem. When taken in the context of the Board class and its constructor, what is 'b'? The compiler wouldn't know. More importantly, IF what you're suggesting was acceptable (AND it's not), you'd have a constructor for a single Board object that would always be called 'b'. What if you also wanted to create Board objects called 'c', 'd', 'e', and 'someName'? With your approach, you wouldn't be able to.

I hope this helps, but I suspect your questions are deeper.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Greg Brannon wrote:You called your constructor from somewhere else, using something like:

Board someName = new Board();

So, when you enter the constructor to create the someName Board object, the keyword 'this' IN THIS SPECIFIC CASE refers to the object someName.




To be precise: During execution of the constructor, "this" refers to the object being constructed, which is the same object that "someName" will refer to after the Board someName = new Board(); statement completes.

It is worth noting that "someName" is not an object. It is a variable whose value is a reference to an object, and during construction of the object, it probably has not been set yet (though it can be if the JVM does instruction reordering).

The keyword 'this' relieves you of having to know the name or instance of the object being acted on.


No. Objects/instances do not have names, so it's not that "this" relieves you of anything. Rather, it is the only way to refer to the "current" object.
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:i don't want to substitute it with something else, but actually i asked this question to clear use of 'this' keyword.
and the first line of the code is commented, please ignore it, i am calling the constructor from some other class like this:

Board b = new Board();

and as you said at this time my this keyword refers to the b object of class Board, isn't it??
and if i use 'b' instead of this, is that right??


The "this" keyword refers to the "current" object during a constructor or instance method (non-static method). It is the only way you can refer to that object in those contexts. If you do b = new Board() or b.someMethod(), there is no way, inside of the constructor or someMethod(), to know that the caller is using variable b. Often times the caller won't use a variable at all.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
okay, got it thanks..
finally: this refer to current object not object...?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:okay, got it thanks..
finally: this refer to current object


Yes.

not object...?


What?
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
i mean if i create more than one instances of a class, that time this refers to the current instance, isn't it??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:i mean if i create more than one instances of a class, that time this refers to the current instance, isn't it??


When we are inside a constructor, it refers to the object currently being constructed.

When we are inside an instance method (a non-static method) it refers to the object whose method is being invoked.

Note that "current instance" does not mean "the last one constructed." It means the one whose code we are executing right now. Note that "current instance" and "this" only have meaning during the execution of a constructor or instance method.

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
okay got it, and i created three instances like you said

Foo f1 = new Foo();
Foo f2 = new Foo();
Foo f3 = new Foo();

and Foo class has a constructor and no methods, so which instance 'this' will refer first, i mean which instance/object will execute first?
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:okay got it, and i created three instances like you said

Foo f1 = new Foo();
Foo f2 = new Foo();
Foo f3 = new Foo();

and Foo class has a constructor and no methods, so which instance 'this' will refer first, i mean which instance/object will execute first?


I'm not sure how I can explain it any more. Inside Foo's constructor, while that constructor is executing, "this" refers to the object being constructed at that moment.

The code executes in the order you write it.
Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
i got it..
thanks for your explanation..
Chetan Sarnad
Greenhorn

Joined: Nov 26, 2011
Posts: 20
Punit Jain wrote:okay got it, and i created three instances like you said

Foo f1 = new Foo();
Foo f2 = new Foo();
Foo f3 = new Foo();

and Foo class has a constructor and no methods, so which instance 'this' will refer first, i mean which instance/object will execute first?


Can you just let me know what will get printed, and your own understanding about this after analyzing the results

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
okay the output is 1 2 3.
and you initialize the

int fooVariable = 10;

again, but it still printing 1 2 3, because you use this keyword here

System.out.println("Value of variable in instance using this:"+this.fooVariable);

so it will always refer to the foo class foovariable, m i right??
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:okay the output is 1 2 3.
and you initialize the

int fooVariable = 10;

again, but it still printing 1 2 3, because you use this keyword here


Yes. For a more complete picture, try this code:

Punit Jain
Ranch Hand

Joined: Aug 20, 2011
Posts: 979
    
    2
i got it finally, thanks for the great explanation..
Jeff Verdegan
Bartender

Joined: Jan 03, 2004
Posts: 6109
    
    6

Punit Jain wrote:i got it finally, thanks for the great explanation..


Great! I'm glad it's clear now.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: problem in when to use this in java
 
Similar Threads
Using the Keyword super
Draughts Board
board not Drawing...
private constructors
Mouse shift event coding - to trigger a predetermined sequence of images to a GUI