Java is an object-oriented (OOP) language. Data is defined as a class, and an object (instance) is taken from the blueprint and used. In this section, you will see the most basic flow of defining a class, creating an object, and handling fields and methods.
Learning Objectives
- Define a class with the
classkeyword - Distinguish between the roles of fields, methods, and constructors.
- Know what
thispoints to - Briefly see the effect of access limiters (
public/private)
Key concepts
1) Class definition
public class Person {
String name;
int age;
void hello() {
System.out.println("Hi, I'm " + name);
}
}
A class is a bundle of state (fields) and behavior (methods).
2) Create object
Person p = new Person();
p.name = "exponent";
p.age = 21;
p.hello();
new creates an object in memory and stores its reference in the variable p.
3) Constructor
public class Person {
String name;
int age;
Person(String name, int age) { //constructor
this.name = name;
this.age = age;
}
}
A constructor is a special method that has no return type. When creating an object, initial values are received. If you do not define it separately, the compiler automatically adds a parameterless default constructor.
4) this keyword
this points to the current object itself. It is often used to clearly distinguish between parameter and field names when they are the same.
this.name = name; //Left=field, right=parameter
See as example
Example 1 — Person.java: Class definition and object creation
public class Person {
String name;
int age;
void hello() {
System.out.println("Hi, I'm " + name + " (" + age + ")");
}
public static void main(String[] args) {
Person p = new Person();
p.name = "exponent";
p.age = 21;
p.hello();
}
}
Execution result
Hi, I'm Jisoo (21)
Memo: If the field is not initialized separately, the default value (0, null, false) is automatically given.
Example 2 — PersonWithCtor.java: constructor and this
public class PersonWithCtor {
String name;
int age;
PersonWithCtor(String name, int age) {
this.name = name;
this.age = age;
}
public static void main(String[] args) {
PersonWithCtor a = new PersonWithCtor("exponent", 21);
PersonWithCtor b = new PersonWithCtor("Minsu", 25);
System.out.println(a.name + ", " + a.age);
System.out.println(b.name + ", " + b.age);
}
}
Execution result
Index, 21
Minsu, 25
Note: The constructor is only called with new.
Example 3 — Counter.java: State by object
public class Counter {
int count;
void increment() {
count++;
}
public static void main(String[] args) {
Counter c1 = new Counter();
Counter c2 = new Counter();
c1.increment();
c1.increment();
c2.increment();
System.out.println("c1=" + c1.count + ", c2=" + c2.count);
}
}
Execution result
c1=2, c2=1
Note: For different instances, fields are managed separately.
Example 4 — Rectangle.java: Adding behavior with a method
public class Rectangle {
int width;
int height;
Rectangle(int w, int h) {
this.width = w;
this.height = h;
}
int area() {
return width * height;
}
int perimeter() {
return 2 * (width + height);
}
public static void main(String[] args) {
Rectangle r = new Rectangle(3, 4);
System.out.println("Area =" + r.area());
System.out.println("Circumference =" + r.perimeter());
}
}
Execution result
area=12
Circumference=14
Memo: Bundling “data + methods that handle that data” into one class is the first step in OOP.
Common Mistakes
- Call like
Person.hello()without creating an object (instance method requires an object) - There is a conflict between field names and parameters, but
this.is omitted → self-assigned - If the constructor name is written incorrectly, it will be considered a method (no return type + class name must be the same)
- Declare and use only variables without
new→NullPointerException - Use
==to compare object reference variables (object is usually.equals())
Cleanup
- A class is a blueprint of state + behavior
- The object is an instance taken from the class as
new - The constructor is a method dedicated to object initialization.
thisis the current object itself
Try it yourself
cd 02_object-oriented/06_class-and_object/src
javac Rectangle.java
java Rectangle