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:14] – [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 |
+ | |||
+ | Member variables will be given default values on instantiation. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. | ||
- | Instance variables have default values. For numbers, the default value is 0, for Booleans it is false, and for object references it is null. 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 40: | 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 51: | 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 61: | 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 '' | ||
- | So, in short, constructors are used to _initialize_ | + | Now when we instantiate a '' |
+ | |||
+ | <code java> | ||
+ | var myClicker = new ClickerCounter(); | ||
+ | </ | ||
+ | |||
+ | '' | ||
+ | |||
+ | In short, constructors are used to initialize | ||
===== Parameterized constructors ===== | ===== Parameterized constructors ===== | ||
+ | |||
+ | The above is a **default constructor** because it has no parameters. It's also possible to define constructors that have parameters. Such a constructor is called a **parameterized constructor**: | ||
+ | |||
+ | <code java> | ||
+ | public class ClickerCounter { | ||
+ | |||
+ | ... | ||
+ | | ||
+ | // Parameterized constructor | ||
+ | public ClickerCounter(int maxCount) { | ||
+ | // initialize member variables | ||
+ | this.maxCount = maxCount; | ||
+ | count = 0; | ||
+ | | ||
+ | // output success | ||
+ | System.out.print(" | ||
+ | System.out.print(" | ||
+ | } | ||
+ | | ||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | You can define as many constructors as you want. You are not required to define a constructor, | ||
===== toString() ===== | ===== toString() ===== | ||
Line 83: | 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.1599876889.txt.gz · Last modified: 2020/09/12 02:14 by mithat