Lesson 3.7: Class Variables and Methods
Lesson 3.7: Class Variables and Methods
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().
Practice Questions
public class Counter {
private int value;
public static int getValue() { return value; }
}
private static int count, what is count?public static int getTotal() and public int getSize(). Which causes a compile error?int x = Box.getTotal();
Box b = new Box(5); int y = b.getSize();
int z = Box.getSize();
Box b = new Box(3); int w = b.getTotal();
Math.sqrt() a static method?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?
Robot r = new Robot("D"); r.getCount();. Which is TRUE?new Coin("penny"); new Coin("dime"); new Coin("nickel");, what does Coin.getMinted() return?Mastery: Class Variables and Methods
public int count = 0; incremented in constructor
private static int count; not incremented in constructor
public static int count = 0; incremented in a separate static method
private static int count = 0; incremented in constructor
public class Calc {
private int result;
public static void reset() { setResult(0); }
public void setResult(int r) { result = r; }
}
new Robot("A"); new Robot("B"); r1 = null;, what does Robot.getCount() return?public static void printCount() { System.out.println(count); } where count is static
public static void printCount() { System.out.println(this.count); }
public static void printCount(int count) { System.out.println(count); }
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.
Message Sent!
Thanks for reaching out. I'll get back to you within 24 hours.
Prefer email? Reach me directly at [email protected]