0
0
Javaprogramming~15 mins

Static variables in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What is a static variable in Java?
A static variable is a variable that belongs to the class, not to any specific object. It is shared by all instances of the class.
touch_appClick to reveal answer
beginner
How many copies of a static variable exist in a Java program?
Only one copy of a static variable exists, regardless of how many objects of the class are created.
touch_appClick to reveal answer
beginner
How do you declare a static variable in Java?
Use the keyword static before the variable type, for example: static int count;
touch_appClick to reveal answer
beginner
Can static variables be accessed without creating an object?
Yes, static variables can be accessed using the class name directly, like <code>ClassName.variableName</code>.
touch_appClick to reveal answer
beginner
What happens to a static variable when you change it through one object?
Changing a static variable through one object changes it for all objects because there is only one shared copy.
touch_appClick to reveal answer
What keyword is used to declare a static variable in Java?
Avolatile
Bfinal
Cstatic
Dconst
How many copies of a static variable exist across all instances of a class?
AOne
BOne per object
CNone
DDepends on the number of threads
How do you access a static variable without creating an object?
AUsing the class name
BUsing the object reference
CUsing the super keyword
DYou cannot access it without an object
If you change a static variable through one object, what happens to other objects?
AThey get an error
BThey keep their own copy
CThey reset the value
DThey see the updated value
Which of these is NOT true about static variables?
AThey are shared by all instances
BEach object has its own copy
CThey belong to the class
DThey can be accessed without creating an object
Explain what a static variable is and how it differs from instance variables in Java.
Describe how you would use a static variable to count how many objects of a class have been created.