Sweater
If this is your sweater, I have it:
Announcements
If you joined the class late, please see:
HW1 is due today.
Please fill out the week 1 survey.
Some Philosophical Reflection
Why bother going to lecture?
… but if that’s not enough
There will be exam recovery points given for attending lecture.
Note: Some policies still shifting (e.g. number of homeworks). Will be finalized soon.
Further Notes for Webcast Viewers
Defining Classes. Lists and Arrays.
6
Lecture 2
CS61B, Fall 2026 @ UC Berkeley
Josh Hug and Manuel Sabin
Defining and Instantiating Classes
Lecture 2, CS61B, Fall 2026
Classes in Java
Lists and Arrays
Classes in Python
Just like Python, Java supports the ability for you to define your own types (classes).
class Dog():
def make_noise(self):
print("bark")
d = Dog()
d.make_noise()
class Dog {
void makeNoise() {
System.out.println("bark");
}
void main() {
Dog d = new Dog();
d.makeNoise();
}
}
Note: Can technically put void main() outside of the class, but I’ll keep it inside. More later.
Classes in Python
Just like Python, Java supports the ability for you to define your own types (classes).
class Dog():
def make_noise(self):
print("bark")
d = Dog()
d.make_noise()
class Dog {
void makeNoise() {
System.out.println("bark");
}
void main() {
Dog d = new Dog();
d.makeNoise();
}
}
Note: Can technically put void main() outside of the class, but I’ll keep it inside. More later.
Object Data
Not all dogs are equal!
Classes, Instance Variables, and Constructors (in Python)
As you’ve hopefully seen in a previous class, instances of objects can contain data. Example:
class Dog():
def __init__(self, size):
self.size = size
def make_noise(self):
if self.size < 10:
print("yip!")
elif self.size < 30:
print("bark.")
else:
print("woooooof!")
maya = Dog(100)
maya.make_noise()
Classes, Instance Variables, and Constructors (in Java)
Java classes are similar. Let’s create an equivalent class in Java.
class Dog():
def __init__(self, size):
self.size = size
def make_noise(self):
if self.size < 10:
print("yip!")
elif self.size < 30:
print("bark.")
else:
print("woooooof!")
maya = Dog(100)
maya.make_noise()
Classes, Instance Variables, and Constructors (in Java)
What differences do you notice?
class Dog():
def __init__(self, size):
self.size = size
def make_noise(self):
if self.size < 10:
print("yip!")
elif self.size < 30:
print("bark.")
else:
print("woooooof!")
maya = Dog(100)
maya.make_noise()
class Dog {
int size;
Dog(int s) {
size = s;
}
void makeNoise() {
if (size < 10) {
IO.println("yipyipyip!");
} else …
}
void main() {
Dog maya = new Dog(100);
maya.makeNoise();
}
}
Class Terminology
Lecture 2, CS61B, Fall 2026
Classes in Java
Lists and Arrays
Defining a Typical Class (Terminology)
class Dog {
int size;
Dog(int s) {
size = s;
}
void makeNoise() {
if (size < 10) {
System.out.println("yipyipyip!");
} else if (size < 30) {
System.out.println("bark. bark.");
} else {
System.out.println("woof!");
}
}
}
Constructor (similar to a method, but not a method). Determines how to instantiate the class.
Instance variable. Can have as many of these as you want.
Instance Method.
Instance Variables in Java
Classes provide a blueprint for instances of that class.
Dog.java provides a blueprint that all Dog objects.
Dog hugeDog = new Dog(150);
hugeDog.size = 5; // size guaranteed to exist
hugeDog.name = "frank"; // will crash, name doesn’t exist
These instances are also called ‘objects’
class Dog {
int size;
Instantiating a Class and Terminology
void main() {
Dog smallDog;
new Dog(20);
smallDog = new Dog(5);
Dog hugeDog = new Dog(150);
smallDog.makeNoise();
hugeDog.makeNoise();
}
Declaration of a Dog variable.
Instantiation of the Dog class as a Dog Object.
Instantiation and Assignment.
Declaration, Instantiation and Assignment.
Invocation of the 150 lb Dog’s makeNoise method.
The dot notation means that we want to use a method or variable belonging to hugeDog, or more succinctly, a member of hugeDog.
Static vs. Instance Members
Lecture 2, CS61B, Fall 2026
Classes in Java
Lists and Arrays
Accessing Classes from Other .java Files
Earlier, we had a main method in the Dog class itself.
Dog.java
class Dog {
int size;
Dog(int s) {
size = s;
}
void makeNoise() {
if (size < 10) {
System.out.println("yipyipyip!");
} else if (size < 30) {
System.out.println("bark. bark.");
} else {
System.out.println("woof!");
}
}
}
Note: Unlike Python, you don’t need to import code from other .java files!
Java automatically scans all folders from a list to see if the desired class exists.
DogInvestigator.java
void main() {
Dog lilDog = new Dog(3);
Dog clifford = new Dog(1000);
clifford.makeNoise();
}
Adding a New Method
Let’s expand the capability of our Dog class so that it can compare dogs.
DogInvestigator.java
void main() {
Dog lilDog = new Dog(3);
Dog clifford = new Dog(1000);
Dog bigger = lilDog.maxDog(clifford);
bigger.makeNoise();
}
Adding a New Method
Let’s expand the capability of our Dog class so that it can compare dogs.
DogInvestigator.java
void main() {
Dog lilDog = new Dog(3);
Dog clifford = new Dog(1000);
Dog bigger = lilDog.maxDog(clifford);
bigger.makeNoise();
}
Dog.java
class Dog {
int size;
Dog(int s) {
size = s;
}
...
Dog maxDog(Dog otherDog) {
if (otherDog.size > this.size) {
return otherDog;
}
return this;
}
}
This this is optional.
This this is required.
Alternate Approach
It is also possible to define the method such that the class itself does the comparison.
Let’s see how we’d do this.
DogInvestigator.java
void main() {
Dog lilDog = new Dog(3);
Dog clifford = new Dog(1000);
Dog bigger = Dog.maxDog(lilDog, clifford);
bigger.makeNoise();
}
Alternate Approach
It is also possible to define the method such that the class itself does the comparison.
Let’s see how we’d do this.
DogInvestigator.java
void main() {
Dog lilDog = new Dog(3);
Dog clifford = new Dog(1000);
Dog bigger = Dog.maxDog(lilDog, clifford);
bigger.makeNoise();
}
Dog.java
class Dog {
int size;
Dog(int s) {
size = s;
}
...
static Dog maxDog(Dog d1, Dog d2) {
if (d1.size > d2.size) {
return d1;
}
return d2;
}
}
Alternate Approach
It is also possible to define the method such that the class itself does the comparison.
Let’s see how we’d do this.
DogInvestigator.java
void main() {
Dog lilDog = new Dog(3);
Dog clifford = new Dog(1000);
Dog bigger = Dog.maxDog(lilDog, clifford);
bigger.makeNoise();
}
Dog.java
class Dog {
int size;
Dog(int s) {
size = s;
}
...
static Dog maxDog(Dog d1, Dog d2) {
if (d1.size > d2.size) {
return d1;
}
return d2;
}
}
By declaring the method “static”, we are saying:
Check Your Understanding #1, hugcode.com/taco
Suppose we have the class shown:
Which of the following usages is appropriate?
class Human {
int consider(int x) {
...
}
static int ponder(int y) {
...
}
}
Human h = new Human();
h.consider(5);
Human.consider(10);
h.ponder(10);
Human.ponder(10);
This is how we’re
tracking attendance.
Check Your Understanding #1
Suppose we have the class shown:
Which of the following usages is appropriate?
class Human {
int consider(int x) {
...
}
static int ponder(int y) {
...
}
}
Human h = new Human();
h.consider(5);
Human.consider(10);
h.ponder(10);
Human.ponder(10);
Compile error
Arguably should be a compile error
This is how we’re
tracking attendance.
Check Your Understanding #1
Suppose we have the class shown:
Suppose we have the following. What do the two bottom calls return?
class Human {
int consider(int x) {
...
}
static int ponder(int y) {
...
}
}
Human h = new Human();
h.consider(5); // returns 5000
Human.ponder(10); // returns 1000
h.ponder(10); // returns ?
Human.ponder(10); // returns ?
Static vs. Non-Static Methods Summary
Key differences between static and non-static (a.k.a. instance) methods:
Dog maya = new Dog(100);
maya.makeNoise();
Dog.makeNoise();
void makeNoise() {
if (size < 10) {
System.out.println("yipyipyip!");
} else if (size < 30) {
System.out.println("bark. bark.");
} else { System.out.println("woof!"); }
}
static void makeNoise() {
System.out.println("Bark!");
}
Static
Non-static
Invocation:
Invocation:
This method cannot access size! Doesn’t exist.
Why Static Methods?
Some classes are never instantiated. For example, Math.
Or in the case of our Dog class, maybe we just liked it aesthetically if we had the class itself do the comparing.
Much nicer than:
Math m = new Math();
x = m.round(x);
Static Variables (are Dangerous)
Classes can also have static variables.
class Dog {
int size;
static String binomen = "Canis familiaris";
Dog(int s) {
size = s;
}
...
}
Static vs. Non-Static
A class may have a mix of static and non-static members.
class Dog {
int size;
static String binomen = "Canis familiaris";
Dog(int s) {
size = s;
}
static Dog maxDog(Dog d1, Dog d2) {
if (d1.size > d2.size)
{ return d1; }
return d2;
}
...
...
void makeNoise() {
if (size < 10) {
System.out.println("yipyipyip!");
} else if (size < 30) {
System.out.println("bark. bark.");
} else {
System.out.println("woof!");
}
}
}
public
Lecture 2, CS61B, Fall 2026
Classes in Java
Lists and Arrays
The public Keyword
Often, you’ll see that in real world code, classes, constructors, variables, and methods are preceded by the keyword public, e.g.
public class Dog {
public int size;
public static String binomen = "Canis familiaris";
public Dog(int s) {
size = size;
}
public static Dog maxDog(Dog d1, Dog d2) {
if (d1.size > d2.size)
{ return d1; }
return d2;
}
...
public static void main(String[] args)
Lecture 2, CS61B, Fall 2026
Classes in Java
Lists and Arrays
Pre-Java 25
Before September 2025:
Compare our Fall 2026 HelloWorld with the Fall 2025 semester’s HelloWorld.
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
}
}
HelloWorld.java (fa25)
HelloWorld.java
void main() {
IO.println("hello world");
}
HelloWorld.java (sp26)
Pre-Java 25
Why public static void main(String[] args)? Well:
public class HelloWorld {
public static void main(String[] args) {
System.out.println("hello world");
}
}
HelloWorld.java (fa25)
HelloWorld.java
void main() {
IO.println("hello world");
}
HelloWorld.java (sp26)
Lists in Java 4.0
Lecture 2, CS61B, Fall 2026
Classes in Java
Lists and Arrays
Lists
In programming languages, a list is an ordered sequence of objects, often represented by comma-separated values in-between brackets.
Example: [3, 6, 9, 12, 15]
Lists support a variety of operations which vary according to the whims of the authors who wrote the code for the list. Some examples:
For more, see wikipedia: https://en.wikipedia.org/wiki/List_(abstract_data_type)
Lists in Python
Python lists have very simple syntax, given below.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
Lists in Java
Let’s try to make a copy of the code below using IntelliJ.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
Lists in Java Attempt #1
Let’s try to make a copy of the code below using IntelliJ.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
void main() {
List L = new List();
}
Can either add import statement to code, or use the IntelliJ option-enter or alt-enter hotkey.
Lists in Java Attempt #2
Let’s try to make a copy of the code below using IntelliJ.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
import java.util.List;
void main() {
List L = new List();
}
Lists in Java Attempt #3
This code finally compiles.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
import java.util.ArrayList;
import java.util.List;
void main() {
List L = new ArrayList();
}
We’ll talk about this distinction shortly.
List Example in Java
We’ve written the equivalent Java program!
Now let’s reflect on that distinction between List and ArrayList.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
import java.util.ArrayList;
import java.util.List;
void main() {
List L = new ArrayList();
L.add("a");
L.add("b");
L.add("c");
System.out.println(L);
}
Abstract Data Types vs. Concrete Implementations
Lecture 2, CS61B, Fall 2026
Classes in Java
Lists and Arrays
List Example in Java
Let’s reflect on that distinction between List and ArrayList.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
import java.util.ArrayList;
import java.util.List;
void main() {
List L = new ArrayList();
L.add("a");
L.add("b");
L.add("c");
System.out.println(L);
}
Alternate Types of List
Java has other types of Lists. Let’s take a peek:
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
import java.util.ArrayList;
import java.util.List;
void main() {
List L = new ArrayList();
...
}
Alternate Types of List
Java has other types of Lists. Let’s take a peek:
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
import java.util.ArrayList;
import java.util.List;
void main() {
List L = new LinkedList();
...
}
List
In 61A/88/E7 Python code, there is no distinction between the abstract idea of a list and an actual list.
In Java, there are many types of lists.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
['a', 'b', 'c']
import java.util.ArrayList;
import java.util.List;
void main() {
List L = new LinkedList();
...
}
Abstract Data Types
Why bother having multiple implementations? What do you think?
List
ArrayList
Linked
List
CopyOnWriteArrayList
...
...
Abstract Data Types
Why bother having multiple concrete implementations of an abstract data type?
We’ll come to explore this concept in much more detail next week.
List
ArrayList
Linked
List
CopyOnWriteArrayList
...
...
For Next Time
Today, we’ve only covered how list usage looked back in the very distant Java 4.0 days.
L = []
L.append("a")
L.append("b")
L.append("c")
print(L)
import java.util.ArrayList;
import java.util.List;
void main() {
List L = new ArrayList();
L.add("a");
L.add("b");
L.add("c");
System.out.println(L);
}
Things for You To Do
That’s all for today’s lecture.
Abstract Data Types vs. Concrete Implementations
Another term used for List in Java is “Abstract Data Type”.
Each implementation, e.g. LinkedList, is known as a “Concrete Implementation”.
List
ArrayList
Linked
List
CopyOnWriteArrayList
...
...