this is a keyword in Java that you can use in your instance methods.
It always refers to the current instance that is executing the method.
So this:
publicclassCourse {
privateStringnumber;
Course(StringsetNumber) {
number=setNumber;
}
}
is equivalent to this:
publicclassCourse {
privateStringnumber;
// We are using this to distinguish between number, the instance variable, and number, the method parameter
Course(Stringnumber) {
this.number=number;
}
}
The example above is one use of this.
However, we’ll usually just go the first route, and choose parameter names that don’t conflict with our instance variable names.
This helps avoid mistakes.
checkstyle will help you with this, complaining (as it does above) that your parameter “hides a field”.
However, there is one place where we will use this.
Let’s go through it together:
The object methods we have been writing so far are known as instance methods:
publicclassPerson {
privateStringname;
publicPerson(StringsetName) {
name=setName;
}
publicStringgetName() {
returnname;
}
}
Personprof=newPerson("Geoff");
Personstudent=newPerson("You");
System.out.println(prof.getName());
System.out.println(student.getName());
Even though they share the same implementation, you can think of eachPerson as having its own getName method
that can access its instance variables, in this case name.
However, Java also allows us to create methods that are attached to the class, not to the instance.
To do that, we use a new keyword: static.
Let’s see how that works:
static class methods are not uncommon.
Particularly when you have methods that don’t need an instance to function properly.
Let’s look at one example of a class that consists almost entirely of static methods,
Math:
https://www.youtube.com/watch?v=P7ZdwGfvJ-8
Practice: Static Adder
Created By: CS 124 Staff
/ Version: 2020.9.0
Create a class named Math. Math should have one public static method named add. add should accept two
int arguments and return their sum.
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
You can also use the static keyword on fields.
One common use for this is to establish constants to make our code more readable and less error-prone.
To do this, we combine static with a keyword that we haven’t seen before, final.
final creates a variable that cannot be changed after it has been set.
We can combine static and final to create a symbolic constant.
Let’s look at how:
Can you create modifiable—that is, non-final—static fields?
Yes.
However.
This is extremely rare, and very easy to get wrong.
We won’t test you on it, but view this walkthrough to see how this works:
publicclassCourse {
}
Practice: Toggler Object
Created By: CS 124 Staff
/ Version: 2021.9.0
Define a public class named Toggler with a single public instance method named toggle that takes no parameters
and returns a boolean.
Toggler should also provide a single public constructor that accepts a boolean argument and sets the initial
state of
the Toggler instance.
Toggler maintains one piece of private state: the boolean.
Calling toggle changes the boolean from true to false or false to true and returns the new (not the
old) state of the boolean.
So, for example:
Note that the internal state should be private.
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.
Homework: Stepper Object
Created By: CS 124 Staff
/ Version: 2022.9.0
Define a class named Stepper with a single public instance method named next that takes no parameters
and returns an int.
Called multiple times, next returns a sequence of values separated by a step amount provided to the constructor.
Stepper should also provide a single public constructor that accepts a int argument and sets the step amount.
Stepper maintains two pieces of private state: the step amount, and the current value, which always starts at 0.
Calling next increments the current value by the step amount, but returns the previous value.
So, for example:
Note that the internal state should be private.
This problem deadline has passed, but you can continue to practice. Experiment! You will not lose credit.
Homework Restricted to Current CS 124 Students
A publicly-accessible version of this content is available at learncs.online.