Win a copy of Head First Agile this week in the Agile forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Calling constructors  RSS feed

 
Tom Gibbins
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi there!

Lets say i have one class, and i want to instantiate an object from another class. The following code is in the first class, Ball:


Here i have a simple class constructor. Next class is PlayArea, where i want to instantiate a ball object:



With the following code, i get a radius return of 0.0. I don't understand why - why does it not set the radius to 5 as defined in the constructor? What is wrong in my code (i of course omitted some code below)?
 
Tony Docherty
Bartender
Posts: 3268
82
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you are creating a new Circle object with a radius of 5, assigning it to the variable 'ball' and then throwing it away.

You should be using super(5); to call the Ball classes super constructor.
 
Tom Gibbins
Ranch Hand
Posts: 36
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Aha! Perfect! Thank you
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My pleasure
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!