Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Restarting a constructor?

 
Jack Nichols
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So i have this constructor to calculate the area and vertex angle of a polygon where you input the number of sides and length of those sides, we aren't even at if/else statements yet, but i was wondering if there is a way to put something after if (tester ==1){ that would a.) clear the notepad which it prints onto and b.) i could start the constructor over again. In my effort to problem solve i tried making it public void RegPoly and then in the if statement just doing RegPoly(); and that worked but is there any way to leave it as a constructor?
Sorry if my code is really messy :|

 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The purpose of a constructor is to ready an object for use. The types of things you are doing in that constructor are not appropriate. You should move any code not intended to initialize the object into methods.
 
Jack Nichols
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay that definitely makes sense and sorry about the code thing, i didn't know how to do that.
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 64708
86
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No problem! Now you know!

Be sure to post your refactored code .
 
arulk pillai
Author
Ranch Hand
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is a bad practice to have program logic in a constructor. Constructor is there to construct object. Add additional methods.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic