KotlinCS 124 LogoJava
PrevIndexNext
Kotlin
Java
  • Implementing a Map : 04/26/2024

  • Streams : 04/25/2024

  • Generics : 04/24/2024

  • Hashing : 04/23/2024

  • Binary Search : 04/22/2024

  • MP3: Course Ratings : 04/19/2024

  • Quicksort : 04/18/2024

  • Merge Sort : 04/17/2024

  • Sorting Algorithms : 04/16/2024

  • MP Debugging Part 1 : 04/15/2024

  • MP2: Course Activity : 04/12/2024

  • Practice with Recursion : 04/11/2024

  • MP Debugging Part 0 : 04/10/2024

  • MP2: API Client : 04/09/2024

  • MP2: API Server : 04/08/2024

  • Trees and Recursion : 04/05/2024

  • Trees : 04/04/2024

  • Recursion : 04/03/2024

  • MP1: Filtering and Search : 04/02/2024

  • MP1: Loading and Sorting : 04/01/2024

  • Lists Review and Performance : 03/29/2024

  • Linked Lists : 03/28/2024

  • Algorithms and Lists : 03/27/2024

  • Continuing MP0 : 03/26/2024

  • Getting Started with MP0 : 03/25/2024

  • Lambda Expressions : 03/22/2024

  • Anonymous Classes : 03/21/2024

  • Practice with Interfaces : 03/20/2024

  • Implementing Interfaces : 03/19/2024

  • Using Interfaces : 03/18/2024

  • Working with Exceptions : 03/08/2024

  • Throwing Exceptions : 03/07/2024

  • Catching Exceptions : 03/06/2024

  • References and Polymorphism : 03/05/2024

  • References : 03/04/2024

  • Data Modeling 2 : 03/01/2024

  • Equality and Object Copying : 02/29/2024

  • Polymorphism : 02/28/2024

  • Inheritance : 02/27/2024

  • Data Modeling 1 : 02/26/2024

  • Static : 02/23/2024

  • Encapsulation : 02/22/2024

  • Constructors : 02/21/2024

  • Objects, Continued : 02/20/2024

  • Introduction to Objects : 02/19/2024

  • Compilation and Type Inference : 02/16/2024

  • Practice with Collections : 02/15/2024

  • Maps and Sets : 02/14/2024

  • Lists and Type Parameters : 02/13/2024

  • Imports and Libraries : 02/12/2024

  • Multidimensional Arrays : 02/09/2024

  • Practice with Strings : 02/08/2024

  • null : 02/07/2024

  • Algorithms and Strings : 02/06/2024

  • Strings : 02/05/2024

  • Functions and Algorithms : 02/02/2024

  • Practice with Functions : 02/01/2024

  • More About Functions : 01/31/2024

  • Errors and Debugging : 01/30/2024

  • Functions : 01/29/2024

  • Practice with Loops and Algorithms : 01/26/2024

  • Algorithms : 01/25/2024

  • Loops : 01/24/2024

  • Arrays : 01/23/2024

  • Compound Conditionals : 01/22/2024

  • Conditional Expressions and Statements : 01/19/2024

  • Operations on Variables : 01/18/2024

  • Variables and Types : 01/17/2024

  • Welcome to CS 124 : 01/16/2024

Static

public class Math {
public static final double PI = 3.141597;
public static int pow(int base, int exponent) {
assert exponent >= 0 : "No support for negative exponents";
int result = 1;
for (int i = 0; i < exponent; i++) {
result *= base;
}
return result;
}
}
System.out.println(Math.PI);
System.out.println(Math.pow(2, 3));

As we continue, we begin by describing a new design keyword: static. We’ll also introduce introduce one new piece of object syntax.

Warm Up Debugging Challenge
Warm Up Debugging Challenge

But first, let’s warm up with another graded debugging challenge!

this
this

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:

public class Course {
private String number;
Course(String setNumber) {
number = setNumber;
}
}

is equivalent to this:

public class Course {
private String number;
// We are using this to distinguish between number, the instance variable, and number, the method parameter
Course(String number) {
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:

public class Course {
private String number;
Course(String setNumber) {
number = setNumber;
}
}

static Methods
static Methods

The object methods we have been writing so far are known as instance methods:

public class Person {
private String name;
public Person(String setName) {
name = setName;
}
public String getName() {
return name;
}
}
Person prof = new Person("Geoff");
Person student = new Person("You");
System.out.println(prof.getName());
System.out.println(student.getName());

Even though they share the same implementation, you can think of each Person 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:

public class Person {
}

static v. Instance
static v. Instance

static class methods cannot access instance variables. Let’s look at why, and the differences between class and instance methods:

public class Person {
private String name;
public Person(String setName) {
name = setName;
}
// Can't access name without an instance!
public static String getName() {
return name;
}
}
Person student = new Person("You");
System.out.println(student.getName());

Uses for Static Methods
Uses for Static Methods

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:

Practice: Static Adder

Created By: Geoffrey Challen
/ 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.

static Fields
static Fields

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:

public class Math {
}

Non-Final static Fields
Non-Final static Fields

Can you create modifiable—that is, non-finalstatic 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:

public class Course {
}

Practice: Toggler Object

Created By: Geoffrey Challen
/ 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.

Homework: Stepper Object

Created By: Geoffrey Challen
/ 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.

More Practice

Need more practice? Head over to the practice page.