Java Static Interview Questions and Answers

Java Static Interview Questions and Answers

Java Online Training1. When to use static operations?

ANS: Static context is necessary is during program start-up because the Java entry point method is static. This means that variables and methods required during start-up may need to be static too, but the principle should be to create a primary core object for your program early and use that to instantiate and interact with other object components of the system.

Static methods are often used as utilities that are not associated with a particular instance of a class.

2. How to use Static Keyword with Abstract Method?

ANS: The use of the abstract keyword in a method declaration along with the modifiers: final, static, native, synchronized, or private is not permitted. n abstract method has no body. A static method belongs to the class and therefore is not inherited. So such a method will never be implemented

3. What is special about static variables?

A: A static variable is shared by all instances of a class. Every instance has a reference to the same variable and can modify it directly. Static instances are declared with the static modifier.

4. Explain the importance of “static” keyword?

ANS: static keyword means only one copy per class if u have a static block or static method it will run first then only other statements like constructor will run. It doesn’t need an object to access its method. Though u can access it using object u can access it using class name. So using object is eliminated.

5. How to call static class members from a different class?

ANS: Calling static members in a separate class is very similar to calling a static method in a separate class. Most importantly, the static member must have a public, package or protected modifier that makes it accessible to the calling class.

6. How to call a static method in a separate class?

ANS: To call a static method in a separate class you should prefix the method reference with the name of the class it belongs to, known as the host class. For example, the String class has value of () methods to convert primitive values to strings:

String Boolean String = String. Value Of (true);

7. How to call a static method from an object reference?

ANS: Yes, it is possible to call a static method from an object reference. The object reference must be the host of the static method.

Unless you happen to have an instance of a class for other purposes, it is generally simpler, clearer and preferable to use a static reference to the host class.

8. What is the difference between static and final keywords?

ANS: The static and final modifiers have quite distinct purposes in Java, static variables must be assigned at compile time and static methods cannot reference instance variables or call instance methods.

The final modifier can be applied to a class definition or method to prevent extension or overriding respectively. This helps the compiler optimise byte code for the class.

9. How to I call the superclass version of an overridden static method?

A: Static methods cannot truly be overridden, if you create static methods with the same signature as those in a super class it will lead to confusing behaviour and is not recommended. It is not possible to use the super keyword in the static context of a subclass because it refers to a instance. Just use the standard static method reference scheme for the super class; the super class name, a dot separator, the method name and arguments, as below.

public class Example Subclass extends Example Superclass {

public static void main(String[] args) {

ExampleSuperclass.testMethod ();
10. Can we have static methods in interface?

ANS: By default, all methods in an interface are decleared as public, abstract. It will never be static.

11. Can we override static method?

ANS: We cannot override static methods. Static methods are belogs to class, not belongs to object. Inheritance will not be applicable for class members

12. What is java static import?

ANS: By using static imports, we can import the static members from a class rather than the classes from a given package.

13. What is a static variable?

ANS: Static keyword can be used with the variables and methods but not with the class but there are static class. Anything declared as static is related to class and not object.

Static variable: Multiples objects of a class shares the same instance of a static variable.

14. What is static class?

Ans: A class cannot be declared static. But a class can be said a static class if all the variables and methods of the class are static and the constructor is private. Making the constructor private will prevent the class to be instantiated. So the only possibility to access is using Class name only.

15. Why static methods cannot access non static variables or methods?

Ans: A static method cannot access non static variables or methods because static methods doesnt need the object to be accessed. So if a static method has non static variables or non static methods which has instantiated variables they will no be initialized since the object is not created and this could result in an error.

For Java Online Training please visit