0
0
Javaprogramming~15 mins

Static vs non-static behavior in Java - Quick Revision & Key Differences

Choose your learning style8 modes available
overviewRecall & Review
beginner
What does the static keyword mean in Java?
The <strong>static</strong> keyword means the member belongs to the class itself, not to any specific object. It can be accessed without creating an object.
touch_appClick to reveal answer
beginner
How do non-static members behave differently from static members?
Non-static members belong to individual objects. Each object has its own copy, so changes affect only that object.
touch_appClick to reveal answer
intermediate
Can a static method access non-static variables directly? Why or why not?
No, a static method cannot access non-static variables directly because non-static variables belong to objects, and static methods belong to the class without any object context.
touch_appClick to reveal answer
intermediate
What happens if you try to use this keyword inside a static method?
You cannot use this inside a static method because this refers to the current object, and static methods do not belong to any object.
touch_appClick to reveal answer
beginner
Give a real-life example to explain static vs non-static behavior.
Think of a school: The school name is static because it is the same for all students (objects). Each student has their own name and grades, which are non-static because they differ per student.
touch_appClick to reveal answer
Which of the following is true about static variables in Java?
AThey can only be accessed through objects.
BThey belong to each object separately.
CThey belong to the class and are shared by all objects.
DThey cannot be accessed inside static methods.
Can a non-static method access static variables directly?
ANo, it cannot access static variables.
BOnly if the static variable is public.
COnly if the method is called from a static context.
DYes, because static variables belong to the class.
What will happen if you try to access a non-static variable inside a static method without an object?
AIt will work fine.
BIt will cause a compile-time error.
CIt will cause a runtime error.
DIt will print default values.
Which keyword is used to declare a static method in Java?
Astatic
Bfinal
Cpublic
Dvoid
If you want a variable to be unique for each object, how should you declare it?
Anon-static (instance variable)
Bfinal
Cstatic
Dabstract
Explain the difference between static and non-static members in Java with examples.
Describe why static methods cannot use the 'this' keyword and what that means for accessing variables.