0
0
Javaprogramming~15 mins

Why static is needed in Java - Quick Recap

Choose your learning style8 modes available
overviewRecall & Review
beginner
What does the static keyword mean in Java?
It means the member (variable or method) belongs to the class itself, not to any specific object of the class.
touch_appClick to reveal answer
beginner
Why do we use static methods in Java?
Because static methods can be called without creating an object, making them useful for utility or helper functions.
touch_appClick to reveal answer
beginner
How does a static variable differ from an instance variable?
A static variable is shared by all objects of the class, while an instance variable is unique to each object.
touch_appClick to reveal answer
intermediate
Can a static method access instance variables directly? Why or why not?
No, because static methods belong to the class, and instance variables belong to objects. Without an object, instance variables don't exist.
touch_appClick to reveal answer
beginner
Give a real-life example of when static is useful.
Think of a calculator app: the add method can be static because it doesnโ€™t need to remember any data, it just adds numbers given to it.
touch_appClick to reveal answer
What does the static keyword do in Java?
AMakes a member belong to the class itself
BMakes a member private
CMakes a member final
DMakes a member abstract
Which of these can be called without creating an object?
AInstance method
BStatic method
CInstance variable
DConstructor
Static variables are shared by:
AAll objects of the class
BOnly one object
CNo objects
DOnly subclasses
Can a static method access instance variables directly?
AYes, always
BOnly if the instance variable is final
CNo, because instance variables belong to objects
DOnly if the static method is private
Why might you make a method static?
ATo save memory by sharing it across objects
BTo make it private
CTo make it abstract
DTo allow calling it without creating an object
Explain why static variables are useful in Java.
Describe a situation where you would use a static method instead of an instance method.