Back

Activity 4.4 Static Variables and Static Methods

divider

Introduction

Activity 4.4

Static Variables and Static Methods

Topics

  • Difference between instance and static members
  • Static variables (class-level data)
  • Static methods (class-level behavior)
  • Why main is static
  • Common AP mistakes involving static context

Instance vs. Static: The Big Idea

So far, all data you've worked with belonged to specific objects (instance variables).

Static means "belongs to the class itself," not to a specific object.

Instance Members (Review)

  • Each object gets its own copy
  • Used to store object-specific data
  • Accessed with objectName.variable

Static Members

  • Shared among ALL objects of the class
  • Stored at the class level, not inside objects
  • Called using ClassName.method()

Example: Counting Objects

class Dog {
private String name;
private static int dogCount = 0; // static: shared across all Dog objects
public Dog(String name) {
this.name = name;
dogCount++; // increments whenever a Dog is created
}
public static int getDogCount() {
return dogCount; // static method can access static variables
}
public String getName() {
return name;
}
}

Static Methods

Static methods do NOT require an object. They are often used for:

  • Utility functions (e.g., conversions)
  • Operations that don't need instance data
  • Working with static variables
class MathTools {
public static int square(int x) {
return x * x;
}
}

Common AP Mistake

Static methods cannot directly access instance variables.

Instance variables belong to objects, not classes.

main is static for this reason — it runs before any objects exist.

Key Terms

Static Variable
Shared by all objects of a class; belongs to the class itself.
Static Method
A method that can be called without creating an object.
Instance Variable
Stored per object; each object has its own copy.
Class Method / Class Variable
Another name for static methods/variables.

'F' → Fullscreen

Objectives

  • icon Distinguish between instance and static members
  • icon Write static variables and static methods
  • icon Count objects using static fields
  • icon Call static utility methods
  • icon Avoid common static-access errors
divider

Activity Tasks

  • icon Create a new project named 4-4-Static-Members.
  • icon Complete each task individually.

Task 1: Counting Objects with Static Variables

Add a static counter to the Student class to track how many objects have been created.

Step 1 — Student.java

Student.java
class Student {
private String name;
private static int studentCount = 0;
public Student(String name) {
this.name = name;
studentCount++;
}
public String getName() {
return name;
}
public static int getStudentCount() {
return studentCount;
}
}

Step 2 — Main.java

Main.java
public class Main {
public static void main(String[] args) {
Student s1 = new Student("Alex");
Student s2 = new Student("Jordan");
Student s3 = new Student("Riley");
System.out.println("Total students: " + Student.getStudentCount());
}
}

Run the program. Notice how static variables track data shared across all objects.


Task 2: Static Utility Methods

Write a utility class with static methods that convert temperatures.

TemperatureConverter.java

TemperatureConverter.java
class TemperatureConverter {
public static double cToF(double c) {
return (c * 9.0 / 5.0) + 32;
}
public static double fToC(double f) {
return (f - 32) * (5.0 / 9.0);
}
}

Main.java

Main.java
public class Main {
public static void main(String[] args) {
System.out.println("20C in F: " + TemperatureConverter.cToF(20));
System.out.println("72F in C: " + TemperatureConverter.fToC(72));
}
}
divider

Sample Output

Sample Output
Total students: 3
20C in F: 68.0
72F in C: 22.222...
divider

Reflection Questions

  1. Explain in your own words the difference between an instance variable and a static variable.
  2. Why can static methods not access instance variables directly?
  3. What is a real-world example of something that should be static?
  4. Why is the main method static?
  5. Describe a situation in which using static methods is preferable to instance methods.
divider

Submission

Submit your completed project and reflection answers to the appropriate dropbox.

Activity Complete