16 Sep 2024
Static Variables
This notebook will explain what static variables are, how they differ from instance variables, and provide examples, including a mini-project with broken code to fix.
1. Introduction to Static Variables
- Static variables belong to the class rather than any particular instance. They are shared across all instances of the class.
- Example:
public class BankAccount {
// Static variable
static double interestRate = 0.03;
// Instance variable
double balance;
// Constructor
public BankAccount(double balance) {
this.balance = balance;
}
// Static method
static void setInterestRate(double newRate) {
interestRate = newRate;
}
}
In this BankAccount
class, interestRate
is a static variable shared by all instances, and setInterestRate()
is a static method that updates this variable.
2. Defining Static Variables
- Static variables are defined using the
static
keyword and accessed using the class name.
- Example:
public class Student {
// Static variable
static String schoolName = "XYZ School";
// Instance variable
String name;
// Constructor
public Student(String name) {
this.name = name;
}
}
// Accessing static variable
System.out.println(Student.schoolName);
Here, schoolName
is a static variable shared by all Student
instances.
3. Static Methods
- Static methods can access static variables but cannot access instance variables directly.
- Example:
public class BankAccount {
static double interestRate = 0.03;
static void updateInterestRate(double newRate) {
interestRate = newRate;
}
}
// Using static method
BankAccount.updateInterestRate(0.04);
The updateInterestRate
method updates the static variable interestRate
.
4. Mini Project: Fix the Code
- Below is a class with broken code. The goal is to fix the class so it correctly implements and accesses static variables and methods.
- Broken code:
public class Vehicle {
static int count = 0;
// Constructor
public Vehicle() {
this.count = count + 1;
}
// Method
public void displayCount() {
System.out.println(count);
}
}
- Task: Debug the
Vehicle
class to ensure count
is properly incremented and displayed. Consider how displayCount()
should be modified if count
is a static variable.
16 Sep 2024
Mutators are used to modify the attribute of an object. They are typically public methods to allow external code to modify the object state.
Naming & Parameters
Mutators are named in the setBlank syntax where “Blank” is the name of the field you want to modify. Mutators, like other methods in Java, take one or more parameters.
Return Type
Since mutators are type void as they do not return a value but rather they modify the object’s state.
Validation
Mutators often include validation logic to ensure that the new values are being set within acceptable bounds. An example of validation being used with mutators can be seen with the setSpeed mutator that doesn’t allow negative values.

Lets take a look at an example!
This example discusses showcases some of the most vital use cases for mutators
// A class representing a Car with attributes like make, model, and speed.
public class Car {
private String brand;
private String model;
private int speed;
// Constructor to initialize attributes
public Car(String brand, String model, int speed) {
this.brand = brand;
this.model = model;
if (speed >= 0) {
this.speed = speed;
} else {
System.out.println("Speed cannot be negative, setting speed to 0.");
this.speed = 0;
}
}
//Mutators
public void setBrand(String brand) {
this.brand = brand;
}
public void setModel(String model) {
this.model = model;
}
public void setSpeed(int speed) {
if (speed >= 0) { //Validation so speed is not negative
this.speed = speed;
} else {
System.out.println("Speed cannot be negative.");
}
}
// Display car details
public void displayCarInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Speed: " + speed + " mph");
}
}
Car myCar = new Car("Honda", "Civic", 100);
myCar.displayCarInfo();
// Modifying attributes mutators
myCar.setSpeed(150);
myCar.setBrand("Rolls Royce");
myCar.setModel("Phantom");
myCar.displayCarInfo();
//Testing Validation with invalid value (Should trigger warning)
myCar.setSpeed(-50);
Brand: Honda
Model: Civic
Speed: 100 mph
Brand: Rolls Royce
Model: Phantom
Speed: 150 mph
Speed cannot be negative.
Popcorn Hack
1. Create a Class
Define a class Person with private fields: String name, int age, and double height. This is done for you.
2. Create Mutators
Write mutator methods setName(String name), setAge(int age) (with validation for non-negative age), and setHeight(double height) (with validation for non-negative height).
3. Write a Constructor
Create a constructor that initializes name, age, and height with provided values.
4. Test the Mutators
In the main method, create a Person object, modify its fields using the mutators, and print the details after each modification.
public class Person {
// Private fields
private String name;
private int age;
private double height;
public void setName(String name) {
this.name = name;
}
public void setAge(int age) {
if (age >= 0) {
this.age = age;
} else {
System.out.println("ERROR: Age cannot be negative.");
}
}
public void setHeight(double height) {
if (height >= 0) {
this.height = height;
} else {
System.out.println("ERROR: Height cannot be negative.");
}
}
Person(String name, int age, double height) {
this.name = name;
this.age = age;
this.height = height;
}
public void displayInfo() {
System.out.println(this.name + "\n" + this.age + "\n" + this.height + "\n");
}
}
Person person = new Person("bob", 1, 1000);
person.displayInfo();
person.setName("dod");
person.setAge(100);
person.setHeight(2);
person.displayInfo();
16 Sep 2024
Anatomy of a Class
This notebook will explain the structure of a class in Java, including attributes, methods, and instantiation. It will also include examples and a mini-project that requires fixing broken code.

1. Introduction to Classes
- A class in Java is a blueprint for creating objects. It defines a data structure that includes methods and attributes.
- Example:
public class Car {
// Attributes
String model;
int year;
// Method
void drive() {
System.out.println("Driving the car.");
}
}
This Car
class has attributes model
and year
, and a method drive()
that prints a message.
2. Attributes and Methods
- Attributes (or fields) are variables that belong to an object.
- Methods are functions that belong to a class.
- Example:
public class Person {
// Attributes
String name;
int age;
// Method
void greet() {
System.out.println("Hello, my name is " + name);
}
}
In this Person
class, name
and age
are attributes, and greet()
is a method.
3. Constructor
- Constructors are special methods used to initialize objects.
- They have the same name as the class and do not have a return type.
- Example:
public class Person {
String name;
int age;
// Constructor
public Person(String name, int age) {
this.name = name;
this.age = age;
}
void greet() {
System.out.println("Hello, my name is " + name);
}
}
This Person
class includes a constructor to initialize name
and age
.
4. Class vs. Instance Variables
- Instance variables are attributes specific to each object.
- Class variables (static variables) are shared among all instances of the class.
- Example:
public class Car {
// Class variable
static int numberOfCars = 0;
// Instance variables
String model;
int year;
// Constructor
public Car(String model, int year) {
this.model = model;
this.year = year;
numberOfCars++;
}
}
Here, numberOfCars
is a class variable that keeps track of how many Car
objects have been created.
5. Mini Project: Fix the Code
- Below is a class with broken code. The goal is to fix the class so it properly initializes and uses instance variables.
-
Broken code:
- Task: Debug the
Book
class so it correctly initializes title
and author
. Consider how the constructor should be modified.
public class Book {
String title;
String author;
// Broken constructor
public Book(String title) {
this.title = title;
}
}
- Task: Debug the
Book
class so it correctly initializes title
and author
. Consider how the constructor should be modified.