§ 02 — Object-Oriented Programming with Java
This lesson explains classes, objects, instance variables, instance methods, object declaration, object creation, and the difference between instance and class members.
A class is a blueprint for creating objects. It describes what data an object can store and what actions it can perform.
For example, a Phone class might describe data such as brand, model, and screen size, and behavior such as power on, call, and send message.
An object is a specific instance of a class. If Phone is the class, then a specific phone model owned by a person can be represented as an object.
Objects usually have:
Software objects are modeled after real-world objects by combining data and behavior.
A class header includes an optional access modifier, the keyword class, and the class name.
public class Student {
// fields and methods go here
}
Fields are variables that belong to the class or object.
public class Student {
int age = 18;
}
Fields are often marked private so other classes cannot change them directly. This protects object data and encourages controlled access through methods.
Common modifiers:
| Modifier | Meaning |
|---|---|
public | Accessible from other classes |
private | Accessible only inside the same class |
protected | Accessible in the same package or subclasses |
| no modifier | Package-level access |
static | Belongs to the class rather than one object |
final | Cannot be changed after assignment |
Instance methods belong to objects. They are usually called after creating an object with new.
Setter methods store values. Getter methods return values.
07 — Activity: Student Class with Instance MethodsPro reviewer — not included in this printout.
ProReviewer — locked
Part of this subject's paid reviewers.
Declaring a variable of a class type does not create an object yet.
Student learner; // declaration only
learner = new Student(); // object creation
Both steps can be combined.
Student learner = new Student();
The new operator reserves memory and calls a constructor.
09 — Activity: Class with Multiple FieldsPro reviewer — not included in this printout.
ProReviewer — locked
Part of this subject's paid reviewers.
An instance variable belongs to one object. Each object gets its own copy.
A static variable belongs to the class. All objects share the same static variable.
public class SchoolRecord {
static int schoolCode = 100;
int studentNumber;
}
If one object changes a static variable, all objects see the updated value.
Pro2 reviewers locked
Part of this subject's paid reviewers. Not included in this printout.
Create a second Student object with different values and call displayInfo() on it.
class Student {
String name;
int age;
double gpa;
void displayInfo() {
System.out.println("Name: " + name);
System.out.println("Age: " + age);
System.out.println("GPA: " + gpa);
}
}
public class Main {
public static void main(String[] args) {
Student s1 = new Student();
s1.name = "Ana Cruz";
s1.age = 19;
s1.gpa = 1.5;
s1.displayInfo();
System.out.println("---");
Student s2 = new Student();
s2.name = "Ben Reyes";
s2.age = 20;
s2.gpa = 1.75;
s2.displayInfo();
}
}Done with this module? Track it — your progress shows on the subject list.
Studying with blockmates? Send them this lesson.
Up next
Lesson 6: Constructors and Constructor Overloading→←Previous: Lesson 4: Methods