Lesson 30 of 34

static மற்றும் final

8 min 35 XP

static field எல்லா objects-க்கும் பொதுவானது, ஒரே ஒரு copy தான். உதாரணம்: எத்தனை objects உருவானது என்று எண்ணுதல். static method-ஐ object இல்லாமல் ClassName.method() என்று அழைக்கலாம். final மதிப்பை மாற்ற முடியாது.

Example: edit me and press Run

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

main() ஏன் static? JVM ஒரு object உருவாக்காமலேயே அதை அழைக்க வேண்டும். இது ஒரு பிரபல interview கேள்வி.

Challenge

+35 XP

Counter class-ல் static int total; ஒவ்வொரு object-ம் தன் சொந்த int value வைத்திருக்கும். increment() method value மற்றும் total இரண்டையும் 1 கூட்டும். main()-ல் n படித்து, இரண்டு Counter objects உருவாக்கி, முதலாவதை n முறையும் இரண்டாவதை 2 முறையும் increment செய்து print செய்யுங்கள்: Input: 3 Output: A: 3 B: 2 Total: 5

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