UNIT 3: CLASS CREATION · TOPIC 3.3
3.3 Anatomy of a Class
The parts of a class in the order the exam expects them, and the access modifiers that make encapsulation work.
What you need to know
- A class declaration:
public class Name { ... }. Inside, in conventional order: instance variables, constructors, methods. - Instance variables hold each object's state. Declare them
private:private String name;Each object gets its own copy. privatemembers are accessible only inside the class.publicmembers are accessible from anywhere. The exam expects private instance variables and public methods/constructors.- Instance variables have default values if not initialized: 0 for int, 0.0 for double, false for boolean,
nullfor references. Local variables have no defaults. - Accessing a private variable from outside the class (
obj.name) is a compile-time error. Use an accessor method instead. - The class's data type name is the class name; you can declare variables of that type:
Student s; finalon an instance variable means it's set once (in the declaration or constructor) and never changed.
Worked example
public class Student
{
// instance variables — private, one set per object
private String name;
private int grade;
private double gpa;
// constructor(s) — 3.4
public Student(String n, int g, double gp)
{
name = n;
grade = g;
gpa = gp;
}
// methods — 3.5
public String getName()
{
return name;
}
}
Exam tip: FRQ 2 rubrics award a point for declaring instance variables private. Forgetting
private costs it every year. Also, a class with instance variables but no constructor is legal — Java provides a default one — but on the FRQ you'll be asked to write one.Going deeper
The nuance, edge cases, and connections that turn a 3 into a 5.
- Conventional order inside a class: instance variables → constructors → methods. Java doesn't require this order, but the exam uses it and FRQ graders expect it. Put private fields at the top.
- Instance variables exist once per object. Two
Studentobjects have two separatenamefields. Methods, by contrast, exist once per class and operate on whichever object they were called on. - Default values apply only to instance (and static) variables: int → 0, double → 0.0, boolean → false, any reference → null. Local variables inside methods have no default and must be assigned before use. This asymmetry is a favorite compile-vs-runtime question.
privatemeans "visible only inside this class." Not subclasses, not other classes in the same file, not anywhere else. Attemptingobj.privateFieldfrom outside is a compile error — the exam's standard "which line won't compile" answer.publicmeans visible everywhere. The exam expects private variables, public methods and constructors. That combination is encapsulation.- A class's type name is its name: you can declare
Student s;, pass aStudentas a parameter, return one, store them in an array or ArrayList. Your class becomes a type as real as String. finalinstance variables are set in the declaration or the constructor and never again. Useful for things that shouldn't change after creation (an ID number).- The class header:
public class Name. The file must be namedName.java. One public class per file.
Mistakes that cost points
- Forgetting
privateon instance variables. A rubric point on every FRQ 2. - Assuming a local variable defaults to 0. Only fields do.
- Accessing a private field from outside. Won't compile. Use the getter.
- Declaring instance variables inside a method. That makes them local. Fields go directly in the class body.
Practice questions
Written in the style of the real exam. Try each one before revealing the answer.
Q1 Consider a class
Box with private int width;. Which of the following statements outside the class will compile?Show answer
Answer: C. Private variables can't be accessed outside the class. A public accessor method is the correct route.
Q2 A class declares
private int count; and has a constructor that does not assign to it. What is the value of count in a newly created object?Show answer
Answer: B. Instance variables get default values; int defaults to 0. (Local variables are the ones that must be assigned first.)
Key vocabulary
- Instance variable
- a variable declared in a class that each object has its own copy of
- private
- accessible only within the class
- public
- accessible from any class
- Default value
- the automatic initial value of an instance variable: 0, 0.0, false, or null