java:initializing_classes_and_constructors
Differences
This shows you the differences between two versions of the page.
Next revision | Previous revision | ||
java:constructor_basics [2020/09/12 02:04] – created mithat | java:initializing_classes_and_constructors [2020/09/16 18:15] (current) – [equals(other)] mithat | ||
---|---|---|---|
Line 1: | Line 1: | ||
- | ====== | + | ====== |
+ | |||
+ | ===== 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. | ||
+ | |||
+ | However, there will be times when you want to override these default values when instantiating objects. For example, with the '' | ||
+ | |||
+ | <file java ClickerCounter.java> | ||
+ | public class ClickerCounter { | ||
+ | |||
+ | // Member variables | ||
+ | private int count; | ||
+ | private int maxCount; | ||
+ | |||
+ | // Accessors and mutators | ||
+ | public int getCount() { | ||
+ | return count; | ||
+ | } | ||
+ | |||
+ | public void setMaxCount(int maxCount) { | ||
+ | if (maxCount > 0) { | ||
+ | this.maxCount = maxCount; | ||
+ | } else { | ||
+ | System.out.println(" | ||
+ | } | ||
+ | } | ||
+ | |||
+ | // Interface methods | ||
+ | public void click() { | ||
+ | if (count < maxCount) { | ||
+ | count++; | ||
+ | } else { | ||
+ | count = 0; | ||
+ | } | ||
+ | } | ||
+ | |||
+ | public void reset() { | ||
+ | count = 0; | ||
+ | } | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | upon instantiation, | ||
+ | |||
+ | One way to do this is to set initial values in the variable declarations: | ||
+ | |||
+ | <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 ===== | ===== Default constructors ===== | ||
- | 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. For example, let's say we are counting votes for a fraudulent election and want to start our counter at 10,000 without anyone being the wiser. To do this, we will need to use a **constructor**: | ||
- | Adding a constructor | + | 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: |
- | Note the syntax: A constructor does not have a return type, is declared with '' | + | <code java> |
+ | public class ClickerCounter { | ||
- | So, in short, constructors are used to _initialize_ | + | ... |
+ | |||
+ | // Default constructor | ||
+ | public ClickerCounter() { | ||
+ | // initialize member variables | ||
+ | maxCount = 9999; | ||
+ | count = 0; | ||
+ | |||
+ | // output success | ||
+ | System.out.print(" | ||
+ | System.out.print(" | ||
+ | } | ||
+ | |||
+ | ... | ||
+ | } | ||
+ | </ | ||
+ | |||
+ | 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> | ||
+ | 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 30: | 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.1599876253.txt.gz · Last modified: 2020/09/12 02:04 by mithat