This is an old revision of the document!
Table of Contents
Java Constructor Basics
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 when instantiating objects. For example, with the ClickerCounter
class we've been developing so far:
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("Invalid maximum count: " + maxCount); } } // Interface methods public void click() { if (count < maxCount) { count++; } else { count = 0; } } public void reset() { count = 0; } }
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 on object instantiation to some other values.
To do this, we can use a constructor: a member method that runs automatically whenever you instantiate the object.
A constructor that does this initialization as well as initializing count
looks like:
public class ClickerCounter { ... // Constructor public ClickerCounter() { maxCount = 9999; count = 0; } ... }
Note the syntax: A constructor does not have a return type, is declared with public
access, and has the same name as the class. In the constructor we also initialized count
is so that someone reading the code would be sure of the initialized value rather than wondering whether we forgot about it.
So, in short, constructors are used to _initialize_ objects. They can be used for other purposes as well, but object initialization is the main reason they were invented.
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:
public class ClickerCounter { ... // Parameterized constructor public ClickerCounter(int maxCount) { this.maxCount = maxCount; count = 0; } ... }
You can define as many constructors as you want. You are not required to define a constructor, but if you define constructors, you must always define a default constructor.
toString()
One thing you'll want to get into the habit of doing with your class definitions is to write a toString()
method. The method should return a string representation of the object. What you want that representation is up to you. The toString()
method will automatically be invoked whenever Java needs a string representation of your object. An example of one for the ClickerCounter
class might look like:
public String toString() { return "count: " + count; }
Now if we want to output the state of a counter using System,out.println()
, we could simply write:
System.out.println(myCounter);
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.
Copyright © 2020 Mithat Konar. All rights reserved.