This week's book giveaway is in the Cloud forum.
We're giving away four copies of Terraform in Action and have Scott Winkler on-line!
See this thread for details.
Win a copy of Terraform in Action this week in the Cloud forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

Is initializing mem vars proper

 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am hoping this is not a difficult question.
 
Ranch Hand
Posts: 3451
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
bryce johnson
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you for clearing that up for me.
 
reply
    Bookmark Topic Watch Topic
  • New Topic