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 revision
Previous revision
java:constructor_basics [2020/09/12 04:57] mithatjava:initializing_classes_and_constructors [2020/09/16 18:15] (current) – [equals(other)] 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.
Line 61: Line 63:
 ===== Default constructors ===== ===== Default constructors =====
  
-Another way to initialize a class is by using a **constructor**: a method that runs automatically whenever you instantiate the object. Constructors are better suited to more complex situations, and they are not limited to just initializing member variables. They can do anything you can normally do in a method. For example, you might want to output that your object was successfully created.+Another way to initialize a class is by using a **constructor**: a method that runs automatically whenever you instantiate the object. Constructors are better suited to more complex situations, and they are not limited to just initializing member variables. You can do anything in a constructor that you can do in a regular method.
  
-A constructor that does does this looks like:+For example, after initializing the state of your object, you might want to output that your object was successfully created. A constructor that does does this looks like:
  
 <code java> <code java>
Line 86: Line 88:
  
 Note the syntax. A constructor: Note the syntax. A constructor:
-  * Does not have a return type (not even ''void''). +  * does not have a return type (not even ''void''). 
-  * Has the same name as the class. +  * has the same name as the class. 
-  * Is declared with ''public'' access.+  * is declared with ''public'' access.
  
 Now when we instantiate a ''ClickerCounter'': Now when we instantiate a ''ClickerCounter'':
Line 142: Line 144:
 </code> </code>
  
-Writing a ''toString()'' instance method is especially important when your class is more complicated than the simple class we've been using here. To understand the magic by which this works, you need to have an understanding of inheritance in Java. But that shouldn't stop you from starting to do this now.+Writing a ''toString()'' instance method is especially important when your class is more complicated than the simple class we've been using here. To understand the magic by which this works, you need to have an understanding of inheritance in Java. But that shouldn't stop you from doing this now. 
 + 
 + 
 +===== equals(other) ===== 
 + 
 +Another method that you can define and is a good habit to get into defining is ''equals''. A very basic definition of `equals` is created automatically, but in almost all cases, you will want to change this functionality. An example of an ''equals'' method for the ''ClickerCounter'' class might look like: 
 + 
 +<code java> 
 +public boolean equals(ClickerCounter other) { 
 +    return (this.count == other.count &&  
 +            this.maxCount == other.maxCount); 
 +
 +</code> 
 + 
 +It's up to you to decide what "equals" means. In many cases, "equals" means that two objects of the same class have the same state. So, in the above definition, for two counter to be equal, both the count and the maxCount must be the same. Now to use this method, assuming both of the variables below refer to valid ''ClickerCounters'': 
 + 
 +<code java> 
 +if (myCounter.equals(yourCounter) { 
 +    System.out.println("Our counters are the same."); 
 +} else { 
 +    System.out.println("Your counter and mine are not the same."); 
 +
 +</code>
  
 Copyright © 2020 Mithat Konar. All rights reserved. Copyright © 2020 Mithat Konar. All rights reserved.
java/initializing_classes_and_constructors.1599886626.txt.gz · Last modified: 2020/09/12 04:57 by mithat

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki