Good practise?

G

Gargoyle

New to coding in Java.

I have a constructor that sets up a JFrame with panels, buttons, etc. and I
want to disable some of the buttons when the frame first appears.
I will also need to disable selected controls during further processing,
probably using a purpose-built method.
I know how to do this.

Don't want to start a heated debate over this but...

My question: is it acceptable programming practise to invoke my purpose-built
method from within a constructor?

Thanks.
 
R

Rhino

Gargoyle said:
New to coding in Java.

I have a constructor that sets up a JFrame with panels, buttons, etc. and
I
want to disable some of the buttons when the frame first appears.
I will also need to disable selected controls during further processing,
probably using a purpose-built method.
I know how to do this.

Don't want to start a heated debate over this but...

My question: is it acceptable programming practise to invoke my
purpose-built
method from within a constructor?

I don't see why it would be a problem to invoke a method that enables or
disables GUI controls from a constructor. I've done so many times and have
no regrets.

Then again, I've never had the particular code examined by an OO design guru
so maybe such an activity violates a design precept of some kind.

To my way of thinking, the constructor's job is to build an instance of an
object. If that object is a GUI, then I don't see why enabling/disabling
controls on that GUI should be avoided within the constructor.
 
D

Daniel Dyer

I don't see why it would be a problem to invoke a method that enables or
disables GUI controls from a constructor. I've done so many times and
have
no regrets.

Then again, I've never had the particular code examined by an OO design
guru
so maybe such an activity violates a design precept of some kind.

To my way of thinking, the constructor's job is to build an instance of
an
object. If that object is a GUI, then I don't see why enabling/disabling
controls on that GUI should be avoided within the constructor.

I agree. However, you should be aware of the potential issues around
calling overrideable methods from a constructor. If the method is private
or final there should be no problem. If not, there could be subtle bugs
if the method is over-ridden by a sub-class. This is because the object
is not guaranteed to have been initialised before the method is invoked
(the superlcass constructor will then be calling a method on the
sub-class).

Dan.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,521
Members
44,995
Latest member
PinupduzSap

Latest Threads

Top