• Post Reply Bookmark Topic Watch Topic
  • New Topic

variable instantiation  RSS feed

 
Anna Hays
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am wondering if it is better to instantiate the class variables inside or outside of the constructor? I have some that has to be instantiated inside the constructor because it throws exceptions and made my code look a bit inconsistent because some of them are outside of the constructor. Does anyone have any thought about this?

Thanks,
Anna
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm going to move this because it is not specific to SCJD. Let's try the Java in General (Intermediate) forum.
 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You could use instance initializer blocks and delare any uncaught nonruntime Exceptions to be thrown by all your constructors:
However, see this note by Doug Dunn
[ April 19, 2006: Message edited by: Barry Gaunt ]
 
Anna Hays
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Umm... I am after if that is a good practice, a SCJD General Consideration.

I have some variables instantiated in the construcor and some are not, it looks a bit messy and inconsistent. I've asked a Senior Developer who has 10 years of experience, he told me this is not very consistent. If I chuck them into the constructor, it makes the constructor very heavy in some classes - looks bad.
 
Paul Clapham
Sheriff
Posts: 22819
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't particularly have any opinion on your question, except "it depends". However you should know that you are not instantiating the variables, you are initializing them.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Am I assuming correctly that you are talking about *instance* variables, not (static) *class* variables?

Personally, I don't have a general problem with some fields being initialized inside, and some outside the constructor.

*However* - if putting all the initialization into the constructor would make the constructor very big, that would make me wonder whether the whole class actually is too big and should be broken down into several classes.
 
Anna Hays
Ranch Hand
Posts: 131
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes I mean initializing instance variables. I've got a Swing class. It has lots of components to make up the screen, I've already divided into a few classes. However, the constructor still need to do a lot. It will make it fairly lengthy if I initialize the components such as JLabels, JTextFields etc in the constructor. I have to initialize my own custom classes in the constructor in order to catch the exceptions. So the variable list in the class looks half initialize and half not - ie, looks inconsistent. I am not sure if this is a good practice.
 
Ilja Preuss
author
Sheriff
Posts: 14112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I find that I typically don't need many of the components as fields - often a local variable in the method that constructs the GUI totally suffices. Why, for example, do you need to remember the JLabels?
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!