• 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:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

problem in when to use this in java

 
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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..

 
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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?
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 563
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
okay, got it thanks..
finally: this refer to current object not object...?
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Yes.

not object...?



What?
 
Punit Jain
Ranch Hand
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it..
thanks for your explanation..
 
Greenhorn
Posts: 22
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 1143
5
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i got it finally, thanks for the great explanation..
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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



Great! I'm glad it's clear now.
 
reply
    Bookmark Topic Watch Topic
  • New Topic