java:initializing_classes_and_constructors
Differences
This shows you the differences between two versions of the page.
Both sides previous revisionPrevious revisionNext revision | Previous revision | ||
java:constructor_basics [2020/09/12 02:25] – [Default constructors] mithat | java:initializing_classes_and_constructors [2020/09/16 18:15] (current) – [equals(other)] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
- | ===== Default constructors | + | ===== Initializing member variables |
- | Instance | + | Member |
However, there will be times when you want to override these default values when instantiating objects. For example, with the '' | However, there will be times when you want to override these default values when instantiating objects. For example, with the '' | ||
- | <file java> | + | < |
public class ClickerCounter { | public class ClickerCounter { | ||
Line 42: | Line 42: | ||
</ | </ | ||
- | upon instantiation, | + | upon instantiation, |
- | To do this, we can use a **constructor**: | + | One way to do this is to set initial values in the variable declarations: |
- | A constructor that does this initialization as well as initializing | + | <code java> |
+ | public class ClickerCounter { | ||
+ | |||
+ | // Member variables | ||
+ | private int count = 0; | ||
+ | private int maxCount = 9999; | ||
+ | |||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | With the above modification, | ||
+ | |||
+ | This approach works fine if your class is fairly simple. | ||
+ | |||
+ | ===== Default constructors ===== | ||
+ | |||
+ | Another way to initialize a class is by using a **constructor**: | ||
+ | |||
+ | 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 53: | Line 72: | ||
... | ... | ||
| | ||
- | // Constructor | + | // Default constructor |
public ClickerCounter() { | public ClickerCounter() { | ||
+ | // initialize member variables | ||
maxCount = 9999; | maxCount = 9999; | ||
count = 0; | count = 0; | ||
+ | | ||
+ | // output success | ||
+ | System.out.print(" | ||
+ | System.out.print(" | ||
} | } | ||
| | ||
Line 63: | Line 87: | ||
</ | </ | ||
- | Note the syntax: A constructor does not have a return type, is declared with '' | + | Note the syntax. A constructor: |
+ | * does not have a return type (not even '' | ||
+ | * has the same name as the class. | ||
+ | * is declared with '' | ||
+ | |||
+ | Now when we instantiate a '' | ||
<code java> | <code java> | ||
Line 69: | Line 98: | ||
</ | </ | ||
- | '' | + | '' |
- | So, in short, constructors are used to _initialize_ | + | In short, constructors are used to initialize |
===== Parameterized constructors ===== | ===== Parameterized constructors ===== | ||
Line 84: | Line 113: | ||
// Parameterized constructor | // Parameterized constructor | ||
public ClickerCounter(int maxCount) { | public ClickerCounter(int maxCount) { | ||
+ | // initialize member variables | ||
this.maxCount = maxCount; | this.maxCount = maxCount; | ||
count = 0; | count = 0; | ||
+ | | ||
+ | // output success | ||
+ | System.out.print(" | ||
+ | System.out.print(" | ||
} | } | ||
| | ||
Line 93: | Line 127: | ||
You can define as many constructors as you want. You are not required to define a constructor, | You can define as many constructors as you want. You are not required to define a constructor, | ||
+ | |||
===== toString() ===== | ===== toString() ===== | ||
Line 109: | Line 144: | ||
</ | </ | ||
- | Writing a '' | + | Writing a '' |
+ | |||
+ | |||
+ | ===== equals(other) ===== | ||
+ | |||
+ | Another method that you can define and is a good habit to get into defining is '' | ||
+ | |||
+ | <code java> | ||
+ | public boolean equals(ClickerCounter other) { | ||
+ | return (this.count == other.count && | ||
+ | this.maxCount == other.maxCount); | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | It's up to you to decide what " | ||
+ | |||
+ | <code java> | ||
+ | if (myCounter.equals(yourCounter) { | ||
+ | System.out.println(" | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | </ | ||
Copyright © 2020 Mithat Konar. All rights reserved. | Copyright © 2020 Mithat Konar. All rights reserved. |
java/initializing_classes_and_constructors.1599877501.txt.gz · Last modified: 2020/09/12 02:25 by mithat