java
- IntroductionInstallation and SetupJava Syntax and StructureRunning Your First ProgramJava VariablesData Types in JavaOperators in JavaJava Input and OutputControl StatementsLoops in JavaMethods in Java (Functions)Arrays in JavaString Handling in JavaOOPS Concept in JavaClasses and ObjectsConstructor and Constructor Overloadingthis KeywordStatic MembersInheritanceAggregation in JavaMethod OverloadingMethod Overridingsuper KeywordFinal Keyword with Class, Method, and VariablesAccess Modifiers in javaEncapsulation in Java Polymorphism in JavaAbstraction in JavaAbstract ClassesInterfaces in JavaDifference between Abstract Class and Interface Nested and Inner Classes Exception HandlingJava PackagesWrapper Classes and AutoboxingJava Collections FrameworkFile Handling in JavaMultithreadingBasics of Java Memory Management and Garbage CollectionJava JDBC
Java Classes and Objects Tutorial for Beginners
Last Updated on: 24th Oct 2025 13:18:00 PM
Welcome to this complete beginner-friendly tutorial on Classes and Objects in Java — the foundation of Object-Oriented Programming (OOP)!
This tutorial explains everything step-by-step with real-life analogies, clear code examples, and practical scenarios you can relate to. By the end, you’ll not only understand what classes and objects are, but also how to use them like a pro.
Let’s start!
What Are Classes and Objects?
|
Term |
Definition |
Real-Life Example |
|---|---|---|
|
Class |
A blueprint or template for creating objects. It defines properties and behaviors. |
A car design on paper (Toyota Camry blueprint) |
|
Object |
An instance of a class. A real, usable thing created from the blueprint. |
A real car built in the factory using that design |
"Think of it like this:
A class is like a recipe for chocolate cake.
An object is the actual cake you bake using that recipe."
1. Creating a Class in Java
Syntax of a Class
class ClassName {
// Fields (data/variables)
// Methods (behaviors/functions)
}
Real-Life Example: Mobile Phone
Let’s create a MobilePhone class.
class MobilePhone {
// Fields (Properties/Attributes)
String brand;
String model;
int storageGB;
double price;
boolean isOn;
// Methods (Behaviors/Actions)
void turnOn() {
isOn = true;
System.out.println(brand + " " + model + " is now ON.");
}
void turnOff() {
isOn = false;
System.out.println(brand + " " + model + " is now OFF.");
}
void makeCall(String contact) {
if (isOn) {
System.com.println("Calling " + contact + "...");
} else {
System.out.println("Phone is OFF. Cannot call.");
}
}
void displayInfo() {
System.out.println("Brand: " + brand);
System.out.println("Model: " + model);
System.out.println("Storage: " + storageGB + " GB");
System.out.println("Price: $" + price);
System.out.println("Status: " + (isOn ? "ON" : "OFF"));
}
}
2. Creating Objects (Instances)
Syntax to Create an Object
ClassName objectName = new ClassName();
Creating Real Phones from the Blueprint
public class PhoneShop {
public static void main(String[] args) {
// Creating objects (real phones)
MobilePhone phone1 = new MobilePhone();
MobilePhone phone2 = new MobilePhone();
// Setting values for phone1
phone1.brand = "Samsung";
phone1.model = "Galaxy S23";
phone1.storageGB = 256;
phone1.price = 899.99;
phone1.isOn = false;
// Setting values for phone2
phone2.brand = "Apple";
phone2.model = "iPhone 15";
phone2.storageGB = 128;
phone2.price = 1099.99;
phone2.isOn = false;
// Using the phones (calling methods)
phone1.turnOn();
phone1.makeCall("Mom");
phone1.displayInfo();
System.out.println(); // empty line
phone2.turnOn();
phone2.makeCall("Friend");
phone2.displayInfo();
phone1.turnOff();
}
}
Output:
Samsung Galaxy S23 is now ON.
Calling Mom...
Brand: Samsung
Model: Galaxy S23
Storage: 256 GB
Price: $899.99
Status: ON
Apple iPhone 15 is now ON.
Calling Friend...
Brand: Apple
Model: iPhone 15
Storage: 128 GB
Price: $1099.99
Status: ON
Samsung Galaxy S23 is now OFF.
3. Key Concepts Explained
|
Concept |
Explanation |
Example |
|---|---|---|
|
Fields |
Variables inside a class that store data (like properties). |
brand, price |
|
Methods |
Functions that define what the object can do. |
turnOn(), makeCall() |
|
Object |
A real instance with its own data. Multiple objects can exist from one class. |
phone1, phone2 |
|
this keyword |
Refers to the current object. Used to avoid naming conflicts. |
See below |
|
new keyword |
Allocates memory and creates a new object. |
new MobilePhone() |
4. Using this Keyword (Important!)
When field names and parameter names are the same, use this to refer to the current object’s field.
Improved Class with this
class MobilePhone {
String brand;
String model;
int storageGB;
double price;
boolean isOn;
// Constructor will be explained next
MobilePhone(String brand, String model, int storageGB, double price) {
this.brand = brand; // this.brand = current object's brand
this.model = model;
this.storageGB = storageGB;
this.price = price;
this.isOn = false;
}
void turnOn() {
this.isOn = true;
System.out.println(this.brand + " " + this.model + " is ON.");
}
void displayInfo() {
System.out.println("Phone: " + this.brand + " " + this.model);
}
}
5. Summary Table
|
Feature |
Class |
Object |
|---|---|---|
|
Definition |
Blueprint / Template |
Instance of the class |
|
Memory |
No memory allocated |
Memory allocated when created |
|
Creation |
Defined using class keyword |
Created using new keyword |
|
Example |
class Car {} |
Car myCar = new Car(); |
|
Can it hold data? |
Defines structure |
Holds actual values |
|
Real-Life |
House design |
Actual house built |
6. Common Interview Questions
|
Question |
Answer |
|---|---|
|
Can we create an object without a class? |
No. Class is required. |
|
Can we have multiple objects of one class? |
Yes! As many as needed. |
|
What happens when new is used? |
Memory is allocated, constructor is called. |
|
Why use this? |
To distinguish between field and parameter with same name. |
7. Practice Exercise (Try This!)
Create a Book class with:
-
Fields: title, author, pages, isAvailable
-
Constructor
-
Methods: borrowBook(), returnBook(), showDetails()
Then create 2 book objects and test them.
Final Words
You’ve now mastered:
-
What classes and objects are
-
How to create and use them
-
Real-life examples (Phone, Student, Car, Book)
-
Constructors, this, and multiple objects
OOP = Real World in Code
You’re now thinking like a Java developer!
Keep coding!
Next: Learn Encapsulation, Inheritance, and Polymorphism to level up! ![]()
.png)