Lesson 3.7: Class Variables and Methods

Unit 3 · Lesson 3.7 · Code Mechanics

Lesson 3.7: Class Variables and Methods

🕑 30–35 min· 8 Practice Questions· 4 Mastery Questions· Output Predictor + Bug Hunt

What You'll Learn

  • Declare and use class (static) variables correctly
  • Explain what static methods can and cannot access
  • Identify static method + instance variable compile errors
  • Recognize the Math class as an example of all-static methods

Key Vocabulary

Term Definition
class variable Declared with static; ONE copy shared by ALL objects (EK 3.7.B.1)
class method Declared with static; belongs to the class, not any specific instance
instance variable No static; each object has its own copy
instance method No static; can access instance variables of its object
static Java keyword making a member belong to the class rather than individual objects
Math class Example of a class with only static methods: Math.abs(), Math.sqrt()

CED EK 3.7.A.1–3.7.B.1

Class methods (static) CANNOT access instance variables or call instance methods without being passed an object reference (EK 3.7.A.1). Class methods CAN access class variables and call other class methods (EK 3.7.A.2). Class variables are declared with static and shared by all instances (EK 3.7.B.1).

Class Variables: Shared State

Class Variable in Action

public class Robot {
    private String name;           // instance variable
    private static int count = 0;  // class variable

    public Robot(String n) { name = n; count++; }

    public static int getCount() { return count; }
    public String getName()       { return name;  }
}

Robot r1 = new Robot("Alpha");
Robot r2 = new Robot("Beta");
System.out.println(Robot.getCount()); // 2

All Robot objects share one copy of count. Each constructor increments it. Static methods called on the CLASS: Robot.getCount().

Calling Rules

Method Type Can Access
Instance method Instance variables + class variables + both types of methods
Class method (static) Class variables + other class methods ONLY -- NOT instance variables or instance methods

Static Method Cannot Use Instance Variable

public class Sensor {
    private int reading;  // instance variable

    // ILLEGAL
    public static int getReading() {
        return reading;  // COMPILE ERROR
    }

    // LEGAL: pass the object as a parameter
    public static int getReading(Sensor s) {
        return s.reading;  // OK -- same class
    }
}

To use instance data in a static method, pass an object of the class as a parameter (EK 3.7.A.1).

AP Trap: Static Method + Instance Variable = Compile Error

public class Vault {
    private int code;
    public static boolean isValid() {
        return code > 0;  // COMPILE ERROR
    }
}

A static method has no this and cannot access any instance variable directly.

AP Trap: Static Variable Is Shared -- Not Per Object

Robot r1 = new Robot("Alpha");
Robot r2 = new Robot("Beta");
// count == 2 for BOTH -- not 1 each

Students frequently assume each object has its own counter. count is ONE variable shared by all.

AP Trap: Can Call Static on an Instance (Confusing but Legal)

Robot r = new Robot("C");
r.getCount();       // compiles but misleading
Robot.getCount();   // preferred

Java allows calling a static method on an instance, but prefer the class-name syntax to make it clear.

Real-World Connection: The Java Math class is a perfect model: you never write new Math(). All methods are static -- no instance needed.

Summary

  • Class variables (static) belong to the class; all objects share ONE copy (EK 3.7.B.1).
  • Class methods (static) cannot access instance variables or call instance methods directly (EK 3.7.A.1).
  • Class methods CAN access class variables and call other class methods (EK 3.7.A.2).
  • To use instance data in a static method, pass an object as a parameter.
  • All methods in Math are class methods: Math.abs(), Math.sqrt(), Math.random().
Tier 2 · AP Practice

Practice Questions

MCQ 1
The following static method will NOT compile. Why?
public class Counter {
    private int value;
    public static int getValue() { return value; }
}
⚠ Predict the answer before reading the options.
A Static methods must return void
B Method should be named getStaticValue
C Static method cannot access instance variable value
D Return type should be double
C is correct. EK 3.7.A.1: static methods cannot access instance variables.
MCQ 2
Which can a static method access WITHOUT an object parameter?
A A private instance variable in the same class
B A private static variable in the same class
C An instance method in the same class
D An instance method in another class
B is correct. EK 3.7.A.2: static methods can access static variables and other static methods.
MCQ 3
After creating 3 Robot objects, where constructor increments private static int count, what is count?
A 1 for r1, 2 for r2, 3 for r3
B 3 -- one shared variable for all objects
C 0 -- static variables can't be modified in constructors
D 3 only for the most recently created
B is correct. static = one shared copy. All 3 constructors incremented the same count.
MCQ 4
Box has public static int getTotal() and public int getSize(). Which causes a compile error?
⚠ Predict the answer before reading the options.
A int x = Box.getTotal();
B Box b = new Box(5); int y = b.getSize();
C int z = Box.getSize();
D Box b = new Box(3); int w = b.getTotal();
C is correct. getSize() is an instance method; calling it on the CLASS without an object is a compile error.
MCQ 5
Why is Math.sqrt() a static method?
A It can only be called once per program
B It returns a double, requiring static access
C It does not depend on any instance state; it just computes from its parameters
D Static methods are always faster
C is correct. Math.sqrt() computes purely from its input, no object state needed.
MCQ 6
I. Static method can access private static variable in same class
II. Static method can call private instance method in same class
III. Instance method can access private static variable in same class
Which are TRUE?
A I only
B I and III only
C II and III only
D I, II, and III
B is correct. I (EK 3.7.A.2) and III are true. II is false: static cannot call instance method without an object.
MCQ 7
Robot r = new Robot("D"); r.getCount();. Which is TRUE?
A Compile error: static methods can't be called on instances
B Runtime error
C Valid but misleading; Java resolves it as Robot.getCount()
D Valid only if count is public
C is correct. Legal but confusing style. Prefer Robot.getCount().
MCQ 8
After new Coin("penny"); new Coin("dime"); new Coin("nickel");, what does Coin.getMinted() return?
A 1
B 2
C 3
D 0
C is correct. minted is static, incremented once per constructor. 3 objects = 3.
PRACTICE WITH A GAME — CHOOSE ONE:
Output Predictor: Static Variables
Predict the output.
Question 1 of 3  ·  Score: 0

Done!
You got 0 of 3 correct.
Bug Hunt: Static vs Instance
Click the buggy line, or 'No bug'.
Question 1 of 3  ·  Score: 0
No bug — code is correct
Done!
You got 0 of 3 correct.
Tier 3 · AP Mastery

Mastery: Class Variables and Methods

MCQ 1
Which correctly counts all Gadget objects created?
A public int count = 0; incremented in constructor
B private static int count; not incremented in constructor
C public static int count = 0; incremented in a separate static method
D private static int count = 0; incremented in constructor
D is correct. Private static, initialized to 0, incremented in constructor.
MCQ 2
Will this compile?
public class Calc {
    private int result;
    public static void reset() { setResult(0); }
    public void setResult(int r) { result = r; }
}
A Valid: static can call any method in same class
B Compile error: static cannot call instance method without object reference
C Valid: setResult is private so static can access it
D Valid: void methods are always accessible
B is correct. EK 3.7.A.1: static cannot call instance methods without an instance.
MCQ 3
After new Robot("A"); new Robot("B"); r1 = null;, what does Robot.getCount() return?
A 1
B 0
C 2
D Compile error
C is correct. Both constructors incremented count. Nulling a reference doesn't decrement.
MCQ 4
Programmer wants a static method to print the object count. Which is CORRECT?
A public static void printCount() { System.out.println(count); } where count is static
B public static void printCount() { System.out.println(this.count); }
C Must be an instance method
D public static void printCount(int count) { System.out.println(count); }
A is correct. Static method can access static variable directly. B is wrong (no this in static). C is wrong (instance not required). D loses the actual class variable.

Get in Touch

Whether you're a student, parent, or teacher — I'd love to hear from you.

Just want free AP CS resources?

Enter your email below and check the subscribe box — no message needed. Students get daily practice questions and study tips. Teachers get curriculum resources and teaching strategies.

Typically responds within 24 hours

Message Sent!

Thanks for reaching out. I'll get back to you within 24 hours.

🏫 Welcome, fellow educator!

I offer curriculum resources, practice materials, and study guides designed for AP CS teachers. Let me know what you're looking for — whether it's classroom materials, a guest speaker, or Teachers Pay Teachers resources.

Email

[email protected]

📚

Courses

AP CSA, CSP, & Cybersecurity

Response Time

Within 24 hours

Prefer email? Reach me directly at [email protected]