This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes Restarting a constructor? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Restarting a constructor?" Watch "Restarting a constructor?" New topic
Author

Restarting a constructor?

Jack Nichols
Greenhorn

Joined: Oct 10, 2012
Posts: 2
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

Joined: Jan 10, 2002
Posts: 60822
    
  65

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.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Jack Nichols
Greenhorn

Joined: Oct 10, 2012
Posts: 2
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

Joined: Jan 10, 2002
Posts: 60822
    
  65

No problem! Now you know!

Be sure to post your refactored code .
arulk pillai
Author
Ranch Hand

Joined: May 31, 2007
Posts: 3219
It is a bad practice to have program logic in a constructor. Constructor is there to construct object. Add additional methods.


Java Interview Questions and Answers Blog | Amazon.com profile | Java Interview Books
 
Consider Paul's rocket mass heater.
 
subject: Restarting a constructor?
 
Similar Threads
Can't Break Out of a Loop
Exception problem.
Help with "scan.next() incompatible types"
operator cannot be applied to ints.
(new to java)variables might not have been initialized