Class members and methods
The instance method maximum(int a, 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? IO.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. |
final ClubMember
john = new ClubMember("John"),
karen = new ClubMember("Karen");
IO.println(john); ❶ // toString() is being
IO.println(karen); // called implicitly |
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])
❶ ❷ ❸ IO.print(...)
|
Class variable (static) out of type
|
|
|
One of 9 overloaded
methods in class |
No. 117
Class vs. instance
|
Q: |
|
|
A: |
|
No. 118
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: Unzip and import the leap year project into your IDE. You'll find a isLeapYear(int year) dummy implementation and corresponding tests in the LeapYearTest class. The project also contains
|
||||
|
A: |
A first implementation reads: This can be simplified to become: NoteThe |
No. 119
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: public class LoopExample { ... public static void whileSquareNumbers(final int limit) { IO.println("Computing square numbers"); int i = 4; while (i < limit) { IO.println("i = " + i + ", i * i = " + i * i); i += 3; } IO.println("Finished computing square numbers"); } ... Its tempting to implement a This implementation is however flawed: If we call
public static void doWhileSquareNumbers(final int limit){ IO.println("Computing square numbers"); int i = 4; if (i < limit) { // Needed !!! do { IO.println("i = " + i + ", i * i = " + i * i); i += 3; } while (i < limit); } IO.println("Finished computing square numbers"); } This Actually a
. |
