Classes & Objects in Java
A class is the blueprint that defines what a type of object knows (its fields) and what it can do (its methods). An object is a single instance built from that blueprint with the new keyword. If Student is the class, then each enrolled learner is an object with its own name and marks. Understanding classes and objects is the gateway to everything object-oriented in Java.
Defining a class
// Student.java
public class Student {
// Fields (state): each object gets its own copy.
String name;
int marks;
// Method (behaviour): operates on this object's fields.
void printResult() {
String status = (marks >= 40) ? "Pass" : "Fail";
System.out.println(name + ": " + status);
}
}
A field describes what an object has; a method describes what it does.
Creating objects with new
public class Main {
public static void main(String[] args) {
Student a = new Student(); // create an object
a.name = "Asha"; // set its fields
a.marks = 72;
Student b = new Student(); // a second, independent object
b.name = "Rohan";
b.marks = 35;
a.printResult(); // Asha: Pass
b.printResult(); // Rohan: Fail
}
}
new Student() allocates a fresh object on the heap and returns a reference to it. a and b are separate objects: changing a.marks never touches b.marks.
The this keyword
Inside a method, this refers to the current object — the one the method was called on. It is most useful when a parameter name matches a field name.
public class Student {
String name;
void setName(String name) {
this.name = name; // this.name = the field; name = the parameter
}
}
Without this, name = name would just assign the parameter to itself and leave the field unchanged.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesInstance vs static members
By default, fields and methods belong to each object (they are instance members). A member marked static belongs to the class itself and is shared by all objects — useful for counters or utility methods.
public class Student {
static int count = 0; // shared across all Student objects
String name;
Student() { // a constructor — runs when an object is created
count++; // every new Student bumps the shared count
}
}
// Student.count tracks how many Student objects exist.
You access a static member through the class: Student.count. (Constructors get their own dedicated tutorial.)
Records — a concise data class (modern Java)
When a class is just a transparent carrier of data, a record removes boilerplate. The compiler generates the constructor, accessors, equals, hashCode, and toString for you.
// One line defines an immutable data holder.
record Point(int x, int y) {}
public class Demo {
public static void main(String[] args) {
var p = new Point(3, 4);
System.out.println(p.x() + ", " + p.y()); // 3, 4 (auto-generated accessors)
System.out.println(p); // Point[x=3, y=4]
}
}
Use a record for simple, immutable data; use a regular class when you need mutable state or richer behaviour.
Common mistakes
- Calling a method on a
nullreference. If you declareStudent s;withoutnew,sisnullands.printResult()throws aNullPointerException. - Confusing the class with its objects. One class can produce many objects; each has its own field values.
- Shadowing a field by forgetting
this. When a parameter and field share a name, you must qualify the field withthis. - Marking everything static. Static is for class-level data; overusing it defeats the purpose of objects.
- Public class name not matching the file. A
public class Studentmust live inStudent.java.
FAQ
What is the difference between a field and a variable? A field is a variable declared inside a class (belonging to objects or the class). A local variable lives inside a method.
When should I use a record? When the type's whole purpose is to bundle a few values immutably, and you do not need to change them after creation.
Keep going
Next, learn how objects are initialised with Constructors & Overloading, or revisit OOP in Java. All tutorials live on the Java hub; for guided practice in Jalgaon, see the Java course.
Learn to design clean classes at Infoplanet, Jalgaon. Join the waitlist for the Java course.
Want to learn this properly?
Join the waitlist for our courses — beginner-friendly, project-first classes in Jalgaon.
Browse coursesFounder, Infoplanet
Atul Kabra founded Infoplanet in 2001 and has spent over two decades teaching programming — C, C++, Java, databases and more — to students across Maharashtra.
Related guides
Arrays in Java
Store many values of one type with Java arrays: declaring, initializing, indexing, iterating, multidimensional arrays, and the Arrays helper class.
The Java Collections Framework
Use the Java Collections Framework — List, Set, and Map with ArrayList and HashMap — plus generics and iteration, to store flexible groups of objects.
Constructors & Overloading
Initialise objects properly with Java constructors — default and parameterized — and reduce duplication with constructor and method overloading.
