Unit 8: Class Methods
Learning Objectives
Students should
- understand the differences between instance methods and class methods.
- be able to define and use a class method.
- know that the
main
method is the entry point to a Java program. - the modifies and parameters required for a
main
method.
Static Methods
Let's suppose that, in our program, we wish to assign a unique integer identifier to every Circle
object ever created. Without using class fields, we might attempt the following.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
|
Then, on every instantiation of circle, we will have to explicitly supply the latest value for id
.
1 |
|
Of course this can be tedious and prone to error especially if we may instantiate Circle
in multiple places. Then we will have to ensure that the latest value of id
is communicated on these places correctly.
As an improvement, we may actually just let the value of the latest id
be stored in the class Circle
itself as a class field.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Then, each time we instantiate a circle, we have to: (i) retrieve the value of lastId
and (ii) update the value of lastId
.
1 2 |
|
While better, this is still prone to error as it relies on the fact that the client should not forget to use and update. Furthermore, it requires the client to update it correctly and not just set the value of lastId
to some random value. But this can be done better if this is the responsibility of the implementer instead. This can be done with the use of class method.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 |
|
- On Line 5, we added a new instance field
id
to store the identifier of the circle. Note that, since the identifier of a circle should not change once it is created, we use the keywordfinal
here. - On Line 6, we added a new class field
lastId
to remember that thelastId
of the latest circle instance. This field is maintained as part of the classCircle
and is initialized to 0. Now this class field isprivate
instead ofpublic
because it is only accessed and updated within the constructor. - On Line 15 and 16, as part of the constructor, we initialize
id
tolastId
and incrementlastId
. We explicitly accesslastId
throughCircle
to make it clear thatlastId
is a class field.
Note that all of the above are done privately beneath the abstraction barrier. In other words, the class field that we have added is actually set to private
. Additionally, now it does not matter what the client does, the value of lastId
will correctly reflect the number of circle that has been created. The client need not even know about the existence of this class field.
Since lastId
is incremented by one every time a circle is created, we can also interpret lastId
as the number of circles created so far. On Line 22-24, we added a method getNumOfCircles
to return its value.
The interesting thing here is that we declare getNumOfCircles
with a static
keyword. Similar to a static
field, a static
method is associated with a class, not to an instance of the class. Such method is called a class method. A class method is always invoked without being attached to an instance, and so it cannot access its instance fields or call other of its instance methods. The reference this
has no meaning within a class method. Furthermore, just like a class field, a class method should be accessed through the class. For example, Circle.getNumOfCircles()
.
Other examples of class methods include the methods provided in java.lang.Math
: sqrt
, min
, etc. These methods can be invoked through the Math
class: e.g., Math.sqrt(x)
.
Non-Static from Static
Recap that for static fields (i.e., class fields), we only have exactly one instance of it throughout the lifetime of the program. More generally, a field or method with modifier static
belongs to the class rather than the specific instance. In other words, they can be accessed/updated (for fields, assuming proper access modifier) or invoked (for methods, assuming proper access modifier) without even instantiating the class.
Think for a moment about the consequence of this. If we have not instantiate the class, it means that there is no instance. The keyword this
captures the current instance. If there is no instance, the keyword this
cannot be referring to any instance. Therefore, it is not reasonable to assign a specific value to this
keyword.
Since we have no reasonable value for this
within the context of static
method, Java actually prevents the use of this
from any method with static
modifier.
1 2 3 |
|
Try it out, you will get the following error.
1 2 3 |
|
The opposite is not true. We can access class fields from non-static methods.
The main
method
The most common class method you will use is probably the main
method.
Every Java program has a class method called main
, which serves as the entry point to the program. To run a Java program, we need to tell the JVM the class whose main
method should be invoked first. In the example that we have seen,
1 |
|
will invoke the main
method defined within the class Hello
to kick start the execution of the program.
The main
method must be defined in the following way:
1 2 |
|
You have learned what public
and static
means. The return type void
indicates that main
must not return a value. We have discussed what final
means on a field, but are not ready to explain what final
means on a method yet. In fact, this is an optional modifier for the main
method.
The main
method takes in an array ([]
) of strings as parameters. These are the command-line arguments that we can pass in when invoking java
. String
(or java.lang.String
) is another class provided by the Java library that encapsulates a sequence of characters.
Class Diagram (Part 3)
Static fields and/or methods are represented by underline in class diagram. The current version of Circle
has the following class diagram.
If the number of constructs for class diagram is getting too much, do not worry, we will have a unit added to the Java Guide specifically to revise class diagram with application on designing solutions to problems.