Java Classes and Objects

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 25 / 50 · English

Java Classes and Objects

Use one blueprint to create independent objects.

Step-by-step explanationsRunnable JavaPractice + answer

Read this topic in தமிழ் ↗.

By the end of this lesson, you can…

  • Explain the key idea behind Classes and Objects.
  • Read and run a complete example, then predict what changes.
  • Recognize the topic-specific mistake discussed below.
01 / The concept

Understand the idea

new creates an object and returns a reference. Variables can point to separate objects or to the same object. Distinguishing an object from a reference helps explain why assigning one reference to another can share mutable state.

02 / Step by step

Explore the details

Java Classes and Objects

A class is the blueprint. An object is the real thing built from that blueprint. One class can produce unlimited objects, each with its own data.

Defining a Class

Code anatomy / கோடின் விளக்கம்

CodeMeaning
classKeyword that defines a class
CarClass name — always start with an uppercase letter
{Opening brace — class body starts here
StringAttribute type
brandAttribute name — belongs to every Car object
}Closing brace — class body ends here

Creating Objects

The class name must match the file name — a class called Car must live in a file called Car.java. Class names always start with an uppercase letter by Java convention.

Java reference fragmentJAVA
class Dog {
    String breed;
    int age;

    void bark() {
        System.out.println(breed + " says: Woof!");
    }
}

Dog d = new Dog();
d.breed = "Labrador";
d.age = 3;
System.out.println(d.breed + ", age " + d.age);  // Labrador, age 3
d.bark();   // Labrador says: Woof!
03 / Read and predict

A complete runnable example

This complete Main.java program focuses on the lesson idea. Reference fragments above may demonstrate individual statements; this example includes the imports, entry point, and supporting types it needs.

Main.java · complete runnable exampleJAVA
public class Main {
    public static void main(String[] args) throws Exception {
        Book first = new Book();
        Book second = new Book();
        first.title = "Java Basics";
        second.title = "Java Practice";
        System.out.println(first.title);
        System.out.println(second.title);
    }
}
class Book { String title; }
Predict first. Then reveal the expected output / வெளியீடு
Java Basics
Java Practice

The output shown was checked by running this complete example. Trace the executed statements in order, then compare with your run.

04 / Learn by changing

Change it. Run it. Understand it.

Load this lesson example into the editor. Change one value or statement, predict the result, then press RUN to execute Java.

Open Java editor ↗
After the editor loads, choose Load this lesson example, then press RUN.

Powered by OneCompiler. Code you run is sent to OneCompiler for execution. For Scanner examples, supply input in the editor I/O panel; for file examples, files exist in its execution environment.

05 / Your turn

Make it your own

Practice challenge

Replace second = new Book() with second = first, then change second.title.

Reveal the answer and reasoning / விடை

Both references now point to the same Book, so reading first.title sees the change made through second.

One mistake to avoid: Assigning an object reference does not clone the object.

Your learning checkpoint

  • Explain the example without reading the output first.
  • Make the change requested in the practice task.
  • Describe why the result changed—or why compilation failed.

Ready to move on? Follow the next lesson in course order, or return to the curriculum to review a prerequisite.

Coding Tamilan · Understand the idea. Try the code. Build the habit.