Lesson 26 of 34

Constructors और this

9 min 30 XP

Constructor class के नाम वाला एक खास method है जिसका कोई return type नहीं होता। यह new पर अपने-आप चलता है। this.name का अर्थ object का field है; सिर्फ़ name 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) Book@1b6d3586 की जगह पढ़ने लायक चीज़ print करेगा।

Challenge

+30 XP

name और salary fields, constructor Employee(String name, int salary) और method int annualSalary() (salary × 12) वाली class Employee लिखें। main() में name और salary पढ़ें और 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.