Lesson 30 of 34

static, final

8 min 35 XP

static field എല്ലാ objects-നും പൊതുവാണ്, അതിന് ഒരു copy മാത്രം, ഉദാഹരണത്തിന് എത്ര objects ഉണ്ടാക്കി എന്ന് എണ്ണാൻ. static method object ഇല്ലാതെ ClassName.method() എന്ന് call ചെയ്യൂ. final value മാറ്റാനാവില്ല.

Example: edit me and press Run

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

main() എന്തുകൊണ്ട് static? ഒരു object-ഉം ഉണ്ടാകുന്നതിന് മുമ്പ് JVM അതിനെ call ചെയ്യണം. ജനപ്രിയ interview ചോദ്യം.

Challenge

+35 XP

static int total-ഉം ഓരോ object-നും സ്വന്തം int value-ഉം ഉള്ള class Counter എഴുതൂ. increment() രണ്ടിലും 1 കൂട്ടുന്നു. main()-ൽ n വായിച്ച്, രണ്ട് Counters ഉണ്ടാക്കി, ആദ്യത്തേത് n തവണയും രണ്ടാമത്തേത് രണ്ട് തവണയും 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.