• Post Reply Bookmark Topic Watch Topic
  • New Topic

calling a constructor from another constructor  RSS feed

 
Richard Innamorato
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Guys,

I was practicing my java skills and came across an exercise in which a non parameter constructor calls a two parameter constructor. I tried a few searches online but they all came back unsuccessful. This is the part I am working on:



I tried calling the two constructor using the line "this(val1, val2)" but I get an error because val1 and val2 are local variables.

Then I tried to use the same signature: "this(int val1, int val2)" but that didn't work either.

If any of you take a couple of minutes to explain this, I would really appreciated.

Thanks,

Richard.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You were nearly there, you can call it using explicit values eg this(3,5); or you can also use class variables or constants.
 
Mike. J. Thompson
Bartender
Posts: 689
17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Innamorato wrote:Hi Guys,

I was practicing my java skills and came across an exercise in which a non parameter constructor calls a two parameter constructor. I tried a few searches online but they all came back unsuccessful. This is the part I am working on:



I tried calling the two constructor using the line "this(val1, val2)" but I get an error because val1 and val2 are local variables.

Then I tried to use the same signature: "this(int val1, int val2)" but that didn't work either.

If any of you take a couple of minutes to explain this, I would really appreciated.

Thanks,

Richard.


You cannot use val1 and val2 in the zero-arg constructor because they are local to the other constructor that takes 2 values. The zero-arg constructor needs to supply default values to the 2-arg constructor. You could hard code values:



which would pass 0 and 0 to the 2-arg constructor. This means that whenever you create a PairOfDice using the no-arg constructor then both die would always be initialised to 1.

You could also come up with some other strategy for deciding what the initial values would be, such as using a random number generator to generate 2 random values for the die.

The key point to note though, is that when the no-arg constructor makes a call to the constructor with 2 parameters it needs to pass 2 values to it, just like if it had been called from elsewhere in your program.
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Innamorato wrote:I tried calling the two constructor using the line "this(val1, val2)" but I get an error because val1 and val2 are local variables.

I think you'll need to explain further, including the precise error message.

Specifically, in order to call this(val1, val2) you need only a constructor (which you appear to have) that takes two int arguments. Unless you plan (or are required) to store those values in instance variables, the compiler shouldn't care one whit whether they're local or not.

However, your "notes" suggest that you think your int constructor will "create a pair of dice that are initially showing the values val1 and val2".

Well it won't.

Winston
 
Richard Innamorato
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you guys.

So the idea is to pass the x number of parameters existing in the constructor that is being called. And one way to do it is by hard coding the values inside the constructors that is making the call. Unless I come up with a better way.

Thank you guys.

 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Richard Innamorato wrote:And one way to do it is by hard coding the values inside the constructors that is making the call. Unless I come up with a better way.

Can you think of one? A no-args constructor is going to be called by:
new PairOfDice();

So: What do you want in such a case?
I can think of one possibility: two "randomly thrown" dice.

And if that's what you want, how do you think you might do it with this()?

Winston
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In that situation, I would consider whether I really need both constructors.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!