File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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
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: 60991
    
  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: 60991
    
  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
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Restarting a constructor?