Lesson 26 of 34

Constructors ಮತ್ತು this

9 min 30 XP

Constructor ಎಂದರೆ class ನ ಹೆಸರಿನ, return type ಇಲ್ಲದ ವಿಶೇಷ method. ಅದು 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.