Lesson 26 of 34

Constructors மற்றும் thisConstructors and this

9 min 30 XP

Constructor என்பது class-ன் பெயரிலேயே உள்ள சிறப்பு method, return type இல்லை. new செய்யும்போது தானாக இயங்கும். this.name என்பது object-ன் field, name என்பது parameter.

A constructor is a special method with the class's name and no return type. It runs automatically on new. this.name means the object's field; plain name is the parameter.

Example: edit me and press Run

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.

toString()-ஐ override செய்தால் System.out.println(object) அழகான text-ஐக் காட்டும்.

Override toString() and System.out.println(object) prints something readable instead of Book@1b6d3586.

Challenge

+30 XP

Employee class: fields name, salary; constructor Employee(String name, int salary); method int annualSalary() (salary × 12). main()-ல் name, salary படித்து இப்படி print செய்யுங்கள்: Input: Ravi 25000 Output: Ravi earns 300000 per year

Write class Employee with fields name and salary, a constructor Employee(String name, int salary) and a method int annualSalary() (salary × 12). In main(), read name and salary and print: Input: Ravi 25000 Output: Ravi earns 300000 per year

Output Loading Java (first time takes a few seconds)
Press Run to see the result here.