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

class Counter लिहा ज्यात एक static int total आणि प्रत्येक object चे स्वतःचे int value असेल. 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.