User Tools

Site Tools


java:initializing_classes_and_constructors

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revisionBoth sides next revision
java:constructor_basics [2020/09/12 04:58] – [Default constructors] mithatjava:constructor_basics [2020/09/12 05:00] – [Initializing member variables] mithat
Line 42: Line 42:
 </file> </file>
  
-upon instantiation, the ''maxCount'' member variable will be initialized to zero. Given that a counter that counts from zero to zero isn't very useful, it would be good if this were automatically initialized to some other value. One way to do this is to set initial values in the member variable definition:+upon instantiation, the ''maxCount'' member variable will be initialized to zero. Given that a counter that counts from zero to zero isn't very useful, it would be good if this were automatically initialized to some other value. 
 + 
 +One way to do this is to set initial values in the variable declarations:
  
 <code java>  <code java> 
Line 55: Line 57:
 </code> </code>
  
-With the above modification, when a ''ClickerCounter'' is instantiated, its ''maxCount'' will be set to 9999. We also explicitly initialized ''count'' to zero in the constructor so that someone reading the code would be sure that the member variable gets the initialized with the value we wanted rather than wondering whether we forgot about initializing it.+With the above modification, when a ''ClickerCounter'' is instantiated, its ''maxCount'' will be set to 9999. We also explicitly initialized ''count'' to zero so that someone reading the code would be sure that the member variable gets the initialized with the value we wanted rather than wondering whether we forgot about initializing it.
  
 This approach works fine if your class is fairly simple. This approach works fine if your class is fairly simple.
java/initializing_classes_and_constructors.txt · Last modified: 2020/09/16 18:15 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki