Java Classes and Objects
Use one blueprint to create independent objects.
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.
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.
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 / கோடின் விளக்கம்
| Code | Meaning |
|---|---|
class | Keyword that defines a class |
Car | Class name — always start with an uppercase letter |
{ | Opening brace — class body starts here |
String | Attribute type |
brand | Attribute 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.
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!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.
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.
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.
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.
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.