codingstuff.io
ExploreTutorialsProblemsCS Subjects
Get Started
ExploreTutorialsProblemsCS Subjects
Get Started
codingstuff.io

Master the art of building software through interactive tutorials, real-world problems, and guided projects.

Pune, Maharashtra, India

codingstuffmail@gmail.com

Product

  • Explore
  • Tutorials
  • Problems
  • CS Subjects

Company

  • About
  • Contact
  • Privacy Policy
  • Terms & Conditions
  • Sitemap

© 2026 codingstuff.io. All rights reserved.

Built with ❤️ for developers everywhere

/
/
All Tutorials
☕

Java Programming

25 / 65 topics
24Java OOP25Java Classes/Objects26Java Class Attributes27Java Class Methods28Java Constructors29Java Modifiers30Java Encapsulation31Java Packages / API
Tutorials/Java Programming/Java Classes/Objects
☕Java Programming

Java Classes/Objects

Updated 2026-05-12
20 min read

Java Classes/Objects

In the previous topic, we introduced the basics of Object-Oriented Programming (OOP) in Java. Now, let's dive deeper into one of the core concepts of OOP: classes and objects. Understanding how to create and manipulate classes and objects is crucial for building robust and maintainable Java applications.

Introduction

In Java, a class is a blueprint for creating objects. It defines a set of properties (attributes) and methods that the created objects can use. An object, on the other hand, is an instance of a class. It has its own state (values of attributes) and behavior (methods).

Classes and objects are fundamental to OOP because they allow you to model real-world entities and their interactions in your code.

Creating Classes

To create a class in Java, you use the class keyword followed by the class name. Here is a simple example:

Car.java
1public class Car {
2 // Attributes
3 String color;
4 int year;
5
6 // Constructor
7 public Car(String color, int year) {
8 this.color = color;
9 this.year = year;
10 };
11
12 // Method to display car details
13 public void displayInfo() {
14 System.out.println("Car Color: " + color);
15 System.out.println("Car Year: " + year);
16 }
17}

In this example, we have a Car class with two attributes: color and year. We also have a constructor to initialize these attributes and a method displayInfo to print the car's details.

Creating Objects

Once you have a class, you can create objects of that class. Here is how you can create an object of the Car class:

Main.java
1public class Main {
2 public static void main(String[] args) {
3 // Create a Car object
4 Car myCar = new Car("Red", 2023);
5
6 // Display car details
7 myCar.displayInfo();
8 }
9}
Output
Car Color: Red
Car Year: 2023

In this example, we create an object myCar of the Car class and initialize it with "Red" as the color and 2023 as the year. We then call the displayInfo method to print the car's details.

Multiple Objects

You can create multiple objects from the same class. Each object will have its own set of attributes, independent of the others. Here is an example:

Main.java
1public class Main {
2 public static void main(String[] args) {
3 // Create multiple Car objects
4 Car car1 = new Car("Blue", 2021);
5 Car car2 = new Car("Green", 2022);
6
7 // Display details of each car
8 car1.displayInfo();
9 System.out.println(); // Print a blank line for separation
10 car2.displayInfo();
11 }
12}
Output
Car Color: Blue
Car Year: 2021

Car Color: Green
Car Year: 2022

In this example, we create two Car objects, car1 and car2, with different colors and years. Each object has its own state, and calling the displayInfo method on each object prints its respective details.

Practical Example

Let's put everything together in a practical example. We'll create a simple program to manage a collection of books:

Book.java
1public class Book {
2 String title;
3 String author;
4 int year;
5
6 public Book(String title, String author, int year) {
7 this.title = title;
8 this.author = author;
9 this.year = year;
10 }
11
12 public void displayInfo() {
13 System.out.println("Title: " + title);
14 System.out.println("Author: " + author);
15 System.out.println("Year: " + year);
16 }
17}
Library.java
1public class Library {
2 public static void main(String[] args) {
3 // Create multiple Book objects
4 Book book1 = new Book("The Great Gatsby", "F. Scott Fitzgerald", 1925);
5 Book book2 = new Book("To Kill a Mockingbird", "Harper Lee", 1960);
6
7 // Display details of each book
8 System.out.println("Book 1 Details:");
9 book1.displayInfo();
10 System.out.println(); // Print a blank line for separation
11
12 System.out.println("Book 2 Details:");
13 book2.displayInfo();
14 }
15}
Output
Book 1 Details:
Title: The Great Gatsby
Author: F. Scott Fitzgerald
Year: 1925

Book 2 Details:
Title: To Kill a Mockingbird
Author: Harper Lee
Year: 1960

In this example, we have a Book class with attributes for the title, author, and year. We create two Book objects in the Library class and display their details.

Summary

  • Classes are blueprints for creating objects.
  • Objects are instances of classes with their own state and behavior.
  • You can create multiple objects from the same class, each with its own attributes.
  • Classes encapsulate data (attributes) and methods that operate on that data.
ConceptDescription
ClassBlueprint for creating objects
ObjectInstance of a class with its own state and behavior
AttributesData associated with an object
MethodsFunctions defined within a class that operate on the object's data

What's Next?

In the next topic, we will explore Java Class Attributes in more detail. We'll learn about different types of attributes, access modifiers, and how to manage them effectively.

Stay tuned for more insights into Java OOP!


PreviousJava OOPNext Java Class Attributes

Recommended Gear

Java OOPJava Class Attributes