• Post Reply Bookmark Topic Watch Topic
  • New Topic

The "this" keyword question  RSS feed

 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

I don't seem to understand the full use of the keyword this in Java . One use that is clear is :

when the programmer wants to refer to the current object ex :


But in the following context i don t seem to grasp it fully :



I understand what the code does i think , except how < this > is used in the first 2 constructors . It seems to be some kind of call to the third constructor but in the case of the first constructor it only has a parameter ( 0 ) . Can anyone explain this to in a friendly way (started learning java/programming recently) ?
 
Paweł Baczyński
Bartender
Posts: 2074
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This means call a constructor of this class with one parameter of int type passing a value 0.
So it calls a constructor defined in line 7. Then this constructor calls: This means call a constructor of this class with two int parameters passing n and 1.
This calls a constructor defined in line 11.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I understand , so if the third constructor takes the place of the second we should have a problem right ?
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can only write this(...); as the first line of a constructor.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I m trying to understand if the order of the constructors matters in this case(i suspect it does not) . When a line with a constructor call is ran from what i understand what constructor is used depends on the parameters inputted (aka 1 parameter => constructor 2 , none => constructor 1 and 2 parameters => constructor 3). But when creating the class can the constructors in this case be written in any order ? (aka starting with the one with 2 parameters and going to the one with no parameters)
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I think you are correct; as you say, it does not matter which order constructors or methods are written in; they are called by their signatures. The rules for overloading are complicated, however.
Beware of initialisers; they are called in the order they are written.
 
Tiberius Marius
Ranch Hand
Posts: 115
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for answering my questions !
 
Campbell Ritchie
Marshal
Posts: 56525
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're welcome
 
Don't get me started about those stupid light bulbs.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!