Java Classes/Objects
Use one blueprint to create independent objects.
முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.
இந்தப் பாடத்தில்…
- 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.
அடிப்படையைப் புரிந்துகொள்ளுங்கள்
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.
விரிவான விளக்கங்கள்
Java Classes மற்றும் Objects
Java ஒரு object-oriented language — எல்லாமே classes மற்றும் objects-ஐ சுத்தி revolve ஆகும்.
Class என்பது object-களுக்கான blueprint. Object என்பது class-ஓட instance.
Class Create பண்றது
class keyword use பண்ணி class create பண்றோம்:
Object Create பண்றது
Class-ல இருந்து object create பண்றதுக்கு new keyword use பண்றோம்:
ஒரு class-ல இருந்து எத்தனை objects வேணும்னாலும் create பண்ணலாம். ஒவ்வொரு object-உம் அதன் சொந்த attributes values வையுங்கள்க்கலாம்.
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!Java-வில் object create செய்ய எந்த keyword பயன்படுத்துவோம்?
- create
- object
- new
- make
Reveal answer / விடை
new
`new` keyword heap memory-ல் object create செய்து constructor-ஐ call செய்யும். Example: `Car myCar = new Car();` — Java 16+ Records-ல் data classes-க்கு boilerplate குறைக்கலாம்.
Java Records (Java 16+): Data-only classes-க்கு boilerplate குறைக்க Java 16 Records வந்தது. record Point(int x, int y) {} — ஒரே line-ல் constructor, getters, equals(), hashCode(), toString() எல்லாம் automatic-ஆக கிடைக்கும். DTOs மற்றும் value objects-க்கு மிக ideal.
முழுமையான உதாரணம்
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.
நீங்களே முயற்சி செய்யுங்கள்
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.
தவிர்க்க வேண்டிய தவறு: Assigning an object reference does not clone the object.
பாடத்தின் சுருக்கம்
- 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.