0
0
Javaprogramming~15 mins

Static methods in Java - Cheat Sheet & Quick Revision

Choose your learning style8 modes available
overviewRecall & Review
beginner
What is a static method in Java?
A static method belongs to the class rather than any object instance. It can be called without creating an object of the class.
touch_appClick to reveal answer
beginner
How do you call a static method in Java?
You call a static method using the class name followed by a dot and the method name, like <code>ClassName.methodName()</code>.
touch_appClick to reveal answer
intermediate
Can a static method access instance variables directly?
No, static methods cannot access instance variables directly because they do not belong to any object instance.
touch_appClick to reveal answer
beginner
Why use static methods? Give a real-life example.
Static methods are useful for actions that don't need object data. For example, a Math.sqrt() method calculates square roots without needing a Math object.
touch_appClick to reveal answer
advanced
What happens if you try to override a static method in Java?
Static methods cannot be overridden. If a subclass defines a static method with the same signature, it hides the superclass method instead.
touch_appClick to reveal answer
How do you declare a static method in Java?
ABy adding the keyword <code>static</code> before the return type
BBy adding the keyword <code>final</code> before the method name
CBy adding the keyword <code>void</code> before the method name
DBy adding the keyword <code>public</code> after the method name
Which of these can a static method access directly?
AInstance variables
BInstance constructors
CNon-static methods
DStatic variables
What is the correct way to call a static method named calculate in class Utils?
AUtils.calculate()
Bnew Utils().calculate()
Ccalculate()
Dthis.calculate()
Can static methods be overridden in Java?
AOnly if marked final
BYes, like instance methods
CNo, they can only be hidden
DOnly in abstract classes
Which keyword is NOT related to static methods?
Afinal
Bnew
Cvoid
Dstatic
Explain what a static method is and how it differs from an instance method.
Describe a situation where using a static method is better than an instance method.