Java Constructors — தமிழ்

</>Coding Tamilan.← Java course & curriculum
Java Masterclass · Lesson 28 / 50 · தமிழ்

Java Constructors

Create an object in a useful starting state.

Step-by-step explanationsRunnable JavaPractice + answer

முக்கிய விளக்கங்கள் தமிழில் உள்ளன. Runnable example மற்றும் கூடுதல் practice notes ஆங்கிலத்தில் உள்ளன.

இந்தப் பாடத்தில்…

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

அடிப்படையைப் புரிந்துகொள்ளுங்கள்

A constructor initializes a newly created object. Its name matches the class name and it has no return type. Use parameters to require the information an object needs from the beginning.

02 / Step by step

விரிவான விளக்கங்கள்

Java Constructors என்னன்னு தெரியுமா?

Constructor என்பது ஒரு special method — object create ஆகும்போது automatically call ஆகும். Object-ஐ initialize பண்றதுக்கு use ஆகுது.

Constructor-ஓட பேர் class பேரோட same-ஆ இருக்கும். Return type இல்லாது.

Constructor Parameters

Constructor-க்கு parameters குடுக்கலாம் — object create செய்யவும்ம்போது values pass பண்ணோம்:

Constructor-ஐ explicitly define பண்ணல-ன்னா Java ஒரு default (empty) constructor provide பண்ணும்.

Java reference fragmentJAVA
class Person {
    String name;
    int age;

    Person(String name, int age) {
        this.name = name;
        this.age = age;
    }

    void greet() {
        System.out.println("Hi, I'm " + name + ", age " + age);
    }
}

Person p1 = new Person("Alice", 25);
Person p2 = new Person("Bob", 30);
p1.greet();  // Hi, I'm Alice, age 25
p2.greet();  // Hi, I'm Bob, age 30

Default constructor என்றால் என்ன?

  1. Empty method
  2. Programmer எழுதாவிட்டால் Java automatically provide செய்யும் no-argument constructor
  3. Static method
  4. Private constructor
Reveal answer / விடை

Programmer எழுதாவிட்டால் Java automatically provide செய்யும் no-argument constructor

Constructor எழுதாவிட்டால் Java automatically no-argument default constructor தரும். ஆனால் parameterized constructor ஒன்று எழுதினால், default constructor automatically கிடைக்காது — explicitly எழுத வேண்டும்.

03 / Read and predict

முழுமையான உதாரணம்

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 {
        Learner learner = new Learner("Kavi", 3);
        System.out.println(learner.name + ": " + learner.level);
    }
}
class Learner {
    String name; int level;
    Learner(String name, int level) { this.name = name; this.level = level; }
}
Predict first. Then reveal the expected output / வெளியீடு
Kavi: 3

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

நீங்களே முயற்சி செய்யுங்கள்

Practice challenge

Construct another Learner with your chosen name and level.

Reveal the answer and reasoning / விடை

Pass the two values to new Learner(name, level). Each object stores its own constructor arguments.

தவிர்க்க வேண்டிய தவறு: void Learner(…) is a method, not a constructor. Once you declare a constructor, Java does not automatically add a no-argument constructor.

பாடத்தின் சுருக்கம்

  • 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.