my dog learned polymorphism*
The moose likes Beginning Java and the fly likes Is initializing mem vars proper Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Is initializing mem vars proper" Watch "Is initializing mem vars proper" New topic
Author

Is initializing mem vars proper

bryce johnson
Greenhorn

Joined: Mar 21, 2003
Posts: 13
When you make your class, is it proper to initialize the member variables. It would seem that member initialization should be handled through a contructor. Does it matter? Is it bad code? Is there valid reason to initialize member variables not through the contructor?
bryce johnson
Greenhorn

Joined: Mar 21, 2003
Posts: 13
I am hoping this is not a difficult question.
Michael Morris
Ranch Hand

Joined: Jan 30, 2002
Posts: 3451
Hi Bryce,

When you make your class, is it proper to initialize the member variables. It would seem that member initialization should be handled through a contructor.

This is where individual coding style comes in, but I personally am a firm beliver in coding so that your intent is known. Member variables can be initialized outside the constructor but I have always preferred to take care of that inside the constructor. Of course, you may have some members that have to be initialized after construction for one reason or another, I generally just declare those in the class and allow default initialization to take place. Of course static members should be initialized outside a constructor since they are classwide.
Michael Morris


Any intelligent fool can make things bigger, more complex, and more violent. It takes a touch of genius - and a lot of courage - to move in the opposite direction. - Ernst F. Schumacher
bryce johnson
Greenhorn

Joined: Mar 21, 2003
Posts: 13
Thank you for clearing that up for me.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Is initializing mem vars proper