Which is the Best Java Web Framework ?

Java - Interview Questions and Answers on Static Methods

Q1.  Can static method access instance variables ?

Ans. Though Static methods cannot access the instance variables directly, They can access them using instance handler.  

Q2.  Does java allow overriding static methods ?

Ans. No. Static methods belong to the class and not the objects. They belong to the class and hence doesn't fit properly for the polymorphic behavior. 

Q3.  How can we run a java program without making any object?

Ans. By putting code within either static method or static block.

Q4.  Can we override static methods ? Why ?

Ans. No. 

Static methods belong to the class and not the objects. They belong to the class and hence doesn't fit properly for the polymorphic behavior. 

A static method is not associated with any instance of a class so the concept of overriding for runtime polymorphism using static methods is not applicable.

Q5.  Can we access instance variables within static methods ?

Ans. Yes.

we cannot access them directly but we can access them using object reference.

Static methods belong to a class and not objects whereas non static members are tied to an instance. Accessing instance variables without the instance handler would mean an ambiguity regarding which instance the method is referring to and hence its prohibited.

Q6.  How can we create objects if we make the constructor private ?

Ans. We can do so through a static public member method or static block.

Q7.  Similarity and Difference between static block and static method ?

Ans. Both belong to the class as a whole and not to the individual objects. Static methods are explicitly called for execution whereas Static block gets executed when the Class gets loaded by the JVM.

Q8.  What will happen if static modifier is removed from the signature of the main method?

Ans. Program throws "NoSuchMethodError" error at runtime .


Subscribe to Java News and Posts. Get latest updates and posts on Java from Buggybread.com
Enter your email address:
Delivered by FeedBurner
comments powered by Disqus




×

Try our new Java Interview Questions Search Tool.

There are more than 1000 questions with practice tests.

It's updated daily and It's Awesome.


×

Try our new Java Practice Test tool.

There are more than 200 questions and expanding quickly. It's updated daily and It's Awesome.