Anatomy of a Class: Fields, Encapsulation, Constructors
AP Computer Science A · Unit 3 — Class Creation
A class is a blueprint; an object is one house built from it
A class definition has three kinds of members: instance variables (the state each object carries), constructors (code that runs once, when the object is created), and methods (the behaviour). Instance variables are declared private; constructors and the methods that outside code needs are declared public. That combination is encapsulation: the object owns its data and no other class can reach in and corrupt it.
A constructor has the same name as the class and no return type at all — not even void. Writing public void Book(String t) does not make a constructor; it silently makes an ordinary method named Book, and the compiler then supplies a no-argument default constructor instead.
Overloading means two constructors (or two methods) share a name but differ in their parameter lists. Java picks the version whose parameter types match the call. One constructor may delegate to another with this(...), which must be the very first statement in the body.
The keyword this is a reference to the object currently executing the method. Its everyday job is to un-shadow: when a parameter has the same name as an instance variable, the parameter wins inside the body, so this.title means the field and plain title means the parameter.
If you write no constructor, Java supplies a no-argument default that leaves every field at its zero value (0, 0.0, false, null). If you write any constructor, that free default disappears.
copies were public, any line in any class could assign b1.copies = -9, and when a negative book count causes an error you would have to hunt for the culprit across the whole program. With private, only the methods inside the Book class can write to it — the scope of the search shrinks from the whole program to one file. That is the real reason, not a convention for looks.Worked example: two constructors, one calling the other
Write a class Book with two private instance variables, title (String) and copies (int). It needs two constructors: one that takes both the title and the number of copies, and one that takes only the title and defaults the number of copies to 1. You must not repeat the assignments in two places.
Solution
Write the full constructor first, then have the short constructor call it with this(...):
public class Book {
private String title;
private int copies;
public Book(String title, int copies) {
this.title = title;
this.copies = copies;
}
public Book(String title) {
this(title, 1);
}
}Three points to notice:
- The two constructors share a name but have different parameter lists — that is valid overloading.
- In the full constructor,
this.titleis the instance variable whiletitleis the parameter. If you dropthis., the linetitle = title;just assigns the parameter to itself, and the instance variable staysnull. this(title, 1);must be on the first line. Placing it after another statement is a compile error.
Common mistake
The costliest error in this problem is shadowed assignment: writingtitle = title; in a constructor whose parameter has the same name. The code still compiles and still runs, with no warning at all, except that every object carries null or 0. How to prevent it: a hard rule — in a constructor body, the left side of every assignment to an instance variable always starts with this.. Its sibling error is public void Book(...) — with void it is an ordinary method, not a constructor.Key takeaway
Keep instance variablesprivate; a constructor has the same name as the class and no return type; overloads are distinguished by their parameter lists; put this. before every instance variable shadowed by a parameter; and if you use this(...), it must be the first line.