Static Methods in Java and the static Keyword - Quiz
Total: 6 questions
1. What is a static method in Java, and how do you declare and call it?
What is a static method in Java, and how do you declare and call it?
A static method is a method declared with the static keyword before its return type, for example static int square(int x). It belongs to the class itself rather than to any object, so it can be called without creating an instance.
The recommended way to call it is through the class name: MathUtils.square(3). Inside the same class the name can be omitted: square(3). Calling it through an object reference (obj.square(3)) compiles, but it is bad style: it suggests that the method depends on the object, and both IntelliJ IDEA and javac -Xlint:static warn about it. Even if the reference is null, no NullPointerException is thrown — the call is bound to the declared type of the variable.
2. What is the difference between a static method and an instance method in Java?
What is the difference between a static method and an instance method in Java?
A static method belongs to the class, is called as ClassName.method() and needs no object. It has no this or super and can directly use only static fields and methods (others only through an object reference). In a subclass it is hidden, not overridden, and the implementation is chosen at compile time by the reference type.
An instance method belongs to a specific object, is called as object.method(), and has access to this, super, and both static and instance fields. It can be overridden, so polymorphism works: the implementation is chosen at runtime by the actual object type. The reverse restriction does not exist — an instance method can freely call static methods of its class.
3. Can a static method call a non-static method? How do you fix "non-static method cannot be referenced from a static context"?
Can a static method call a non-static method? How do you fix "non-static method cannot be referenced from a static context"?
Yes, but only through an object reference. A static method such as main runs without an object, so calling an instance method by its bare name — greet(); — is rejected with non-static method greet() cannot be referenced from a static context. The same kind of error appears when you use an instance field or this in a static method.
There are two fixes. If the method uses object state, create an object and call the method on it: new Greeter().greet();. If the method does not touch instance fields, make it static. Blindly adding static only moves the error to the field: non-static variable name cannot be referenced from a static context.
4. Can you override or overload a static method in Java?
Can you override or overload a static method in Java?
You cannot override it. A static method with the same signature in a subclass hides the parent's method (method hiding). The implementation is chosen at compile time by the reference type: for Parent p = new Child(); the call p.type() runs the Parent version, with no polymorphism. Putting @Override on a static method is a compile error, and abstract static fails with "illegal combination of modifiers", because an abstract method must be overridden.
You can overload it. Overloading only requires a different parameter list: static int sum(int a, int b) and static double sum(double a, double b) can live side by side in one class — this is how Math.max is overloaded.
5. Can a record declare static methods, and what are they used for?
Can a record declare static methods, and what are they used for?
Yes. A record may declare static methods and static fields — unlike additional instance fields, which are not allowed outside the record components. The typical use is a static factory method that creates the record instead of the constructor, or a predefined constant: static Point of(int x, int y), static final Point ORIGIN = new Point(0, 0);.
From outside, such a method is called through the record name: Point.of(3, 4). Inside the record it can also be called from instance methods without the class name — just like in a regular class, there is no restriction in that direction.
6. When should you make a method static, and why is the main method static?
When should you make a method static, and why is the main method static?
A static method fits when its result depends only on its parameters, not on the state of an object. Typical cases: utility methods (Math.max(a, b), Arrays.sort(arr), Integer.parseInt("42")), static factory methods instead of a constructor (List.of(1, 2, 3), LocalDate.of(2024, 1, 1)), and working with static fields (counters, caches). If a method reads or changes an object's fields, make it an instance method: overusing static turns code procedural and makes testing harder — a static call is difficult to replace with a mock.
The main method is static because the JVM starts a program before any object of your class exists: a static main can be invoked through the class name without creating an instance, which is why it serves as the entry point.