Class members and methods
The instance method maximum(int 1, int b)
returns the larger one of two integer arguments a
and b
:
|
final Helper instance = new Helper(); // Why do we need an instance just for // computing the maximum of two values? System.out.println("Maximum: " + instance.maximum(-3, 5)); |
Maximum: 5 |
Observation: The instance's state is irrelevant for finding the maximum of two values.
This common situation leads to another category of methods being
independent of any instances. They are being defined on class level by
virtue of the static
keyword. We call these
»class methods« for short:
-
Each club member has got a name.
-
Each member has got an ascending unique membership number.
-
The overall club's member count needs to be accounted for.
Solution:
-
Class level: Advance club's member count by each new member.
-
Instance level: New members receive name and current member count plus 1.
public class ClubMember {
final private String name;
public ClubMember(final String name) {
this.name = name;
}
public String toString() {
return "Member " + name;
}
}
|
Member John Member Karen |
public class ClubMember { static ❶ private int memberCount = 0; final private int memberNumber; ❷ final private String name; public ClubMember(final String name) { this.name = name; memberNumber = ++memberCount; ❸ } public String toString() { return "Member " + name + ", member number " + memberNumber ❹; } }
The keyword This is similar to our |
|
|
|
On creating a new instance of
|
|
Accessing an individual member's name and membership number. |
|
Member John, member number 1 Member Karen, member number 2 |
As an aside: At runtime the last two lines are strictly equivalent to:
This is due to the chosen |
|
Member Karen, member number 2 Club's member count:3 |
- Class Variable
-
{class name}.{variableName}
- Class Method
-
{class name}.{methodName}([parameter])
❶ ❷ ❸ System.out.print(...)
Class variable (static) out of type |
|
One of 9
overloaded methods in class |
No. 99
Class vs. instance
Q: |
|
A: |
|
No. 100
Distinguishing leap- and non-leap years
Q: |
In Leap years you already started a leap year related exercise. This exercise is about wrapping your implementation into a method:
Write unit tests prior to actually implementing
After finishing you should also be able to test your implementation manually:
This should produce the following output: Is 1800 a leap year? false Is 2000 a leap year? true Is 2016 a leap year? true |
A: |
We start by a dummy implementation:
This enables us writing unit tests beforehand:
Obviously all negative tests will fail. We now implement the desired method:
This one is easy to read. Experienced programmers however prefer compact code:
|
No. 101
A method for printing square numbers using for
, while
and do
... while
Q: |
You already did exercises on printing mathematical tables. This exercise is about decomposing tasks into methods thereby improving code readability. We also explore using different loop types. Consider the following example:
Re-implement the above code in two different ways:
Caveat: The Which of these three loop implementations is your favourite? Give a reason. |
A: |
The while loop is actually quite straightforward to implement:
Its tempting to implement a
This implementation is however flawed: If we call
This Actually a
. |