Liang Chapter 9: Defining Classes, Creating Objects, Constructors, Getters/Setters, the this Keyword, and Encapsulation
A class is a template (blueprint) for creating objects. It defines the data fields (also called instance variables or properties) and methods that an object will have. An object is an instance of a class. (Liang, Ch. 9)
A class in Java can contain data fields (variables that store the state) and methods (functions that define the behavior). (Liang, Section 9.2)
In UML (Unified Modeling Language), a class is represented as a rectangle divided into three compartments: class name, data fields, and methods. (Liang, Section 9.2)
The - sign indicates private (accessible only within the class). The + sign indicates public (accessible from any class). (Liang, Section 9.2)
| Class | Object |
|---|---|
| A blueprint or template | An instance created from the class |
| Defined once | Multiple objects can be created |
| No memory allocated until an object is created | Memory is allocated when created with new |
Example: Circle (the class) | Example: myCircle (an object) |
new keyword. You can create many objects from one class. (Liang, Section 9.2)public class Student { String name; }class = Student { String name; }def class Student { name: String; }new class Student { String name; }class keyword, followed by the class name and a body in curly braces. Data fields are declared inside the class body. (Liang, Section 9.2)An object is created from a class using the new keyword. The new keyword invokes the class's constructor, allocates memory for the object, and returns a reference to it. (Liang, Section 9.3)
A variable that holds an object is actually a reference variable. It stores the memory address (reference) of the object, not the object itself. (Liang, Section 9.4)
Stack: Stores the reference variable (e.g., myCircle → address)
Heap: Stores the actual object (e.g., Circle object with radius: 5.0)
The reference on the stack points to the object on the heap.
| Primitive Variable | Reference Variable |
|---|---|
| Stores actual value | Stores memory address of object |
int x = 5; | Circle c = new Circle(); |
| Default: 0, false, etc. | Default: null |
| Stored on the stack | Reference on stack, object on heap |
You access an object's data fields and methods using the dot operator (.). (Liang, Section 9.3)
If a reference variable does not point to any object, it holds the value null. Attempting to access a member through a null reference causes a NullPointerException. (Liang, Section 9.4)
new keyword do?new keyword allocates memory on the heap for the object, invokes the constructor to initialize it, and returns a reference. (Liang, Section 9.3)nullundefinednull, meaning it does not reference any object. (Liang, Section 9.4)What is the output? (Hint: c2 = c1 copies the reference, not the object)
myCircle might not have been initialized (no new was used)myCircle is declared but never assigned an object with new Circle(). Local variables must be initialized before use. The compiler will report the error. (Liang, Section 9.4)A constructor is a special method that is invoked to initialize an object when it is created using the new keyword. It has the same name as the class and no return type (not even void). (Liang, Section 9.5)
A constructor with no parameters is called a no-arg (no-argument) constructor. (Liang, Section 9.5)
A constructor can take parameters to set initial values for the object's data fields. (Liang, Section 9.5)
A class can have multiple constructors with different parameter lists. This is called constructor overloading. Java determines which constructor to invoke based on the arguments passed. (Liang, Section 9.5)
If you do not define any constructor in your class, Java automatically provides a default no-arg constructor that initializes data fields to their default values. However, if you define any constructor (even a parameterized one), Java does NOT provide the default no-arg constructor. (Liang, Section 9.5)
voidintvoid. If you add a return type, it becomes a regular method, not a constructor. (Liang, Section 9.5)new MyClass()?What is the output?
The keyword this refers to the current object — the object whose method or constructor is being invoked. It is commonly used to distinguish between data fields and parameters that have the same name. (Liang, Section 9.6)
If the parameter name is the same as the data field name, the parameter hides (shadows) the data field. Using this.radius explicitly refers to the data field. (Liang, Section 9.6)
| Usage | Purpose | Example |
|---|---|---|
this.field | Refer to the current object's data field | this.radius = radius; |
this.method() | Call another method of the current object | this.getArea(); |
this(args) | Call another constructor from a constructor | this(1.0); |
this(args) must be the first statement in the constructor. You cannot call it after other statements. (Liang, Section 9.6)
this refer to in a method?this is a reference to the current object — the one on which the method was called. (Liang, Section 9.6)After new Student("Ali"), what is the value of the name field?
"Ali" — it works correctlynull — the parameter shadows the field; should be this.name = name;name = name assigns the parameter to itself. The data field is never set, so it remains null. The fix is this.name = name;. (Liang, Section 9.6)Encapsulation is one of the fundamental OOP principles. It means making data fields private and providing public getter and setter methods to access and modify them. This protects the data from direct external access and enables data validation. (Liang, Section 9.9)
Data protection: Prevents external code from setting invalid values directly.
Flexibility: The internal implementation can change without affecting external code.
Maintainability: Easier to debug and maintain — all access goes through methods.
A getter method returns the value of a data field. A setter method sets a new value, optionally with validation. (Liang, Section 9.9)
For a data field xyz, the getter is getXyz() and the setter is setXyz(value). For a boolean field, the getter is isXyz(). (Liang, Section 9.9)
| Modifier | Class | Package | Subclass | World |
|---|---|---|---|---|
private | ✓ | ✗ | ✗ | ✗ |
| (default) | ✓ | ✓ | ✗ | ✗ |
protected | ✓ | ✓ | ✓ | ✗ |
public | ✓ | ✓ | ✓ | ✓ |
private?private prevents direct access from outside the class. Access is controlled through getter and setter methods, enabling validation. (Liang, Section 9.9)boolean field called active?getActive()active()isActive()hasActive()is instead of get. So the getter is isActive(). (Liang, Section 9.9)radius has private access in Circleradius is declared private in the Circle class, it cannot be accessed directly from another class. You must use c.setRadius(10.0) instead. (Liang, Section 9.9)A static variable or method belongs to the class, not to any particular object. It is shared by all objects of the class. (Liang, Section 9.7)
A static variable is shared among all instances of a class. If one object changes the static variable, all other objects see the updated value. (Liang, Section 9.7)
| Instance | Static | |
|---|---|---|
| Belongs to | Each object | The class |
| Access | objectName.member | ClassName.member |
| Memory | One copy per object | One copy shared by all |
| Example | radius | numberOfObjects |
• A static method cannot access instance variables or call instance methods directly.
• An instance method can access both static and instance members.
• A static method cannot use the this keyword. (Liang, Section 9.7)
this keywordthis and no way to access instance variables directly. It can only access static members. (Liang, Section 9.7)ClassName.methodName()this.methodName()main methodClassName.methodName(). While calling through an object reference works, it is misleading and not recommended. (Liang, Section 9.7)What is the output?
radius from a static methodgetArea() is static but tries to access radius, which is an instance variable. A static method cannot reference instance variables. Remove the static keyword to fix. (Liang, Section 9.7)