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.