Q)
Will jvm executes static methods by default like static variables?
Jvm will not execute
static methods by itself. They are executed only if they are called explicitly
by developer either from main method or from static variable as its assigment
statement or from static block.
Q)
What is need of static method?
If
you need to access a method without creating an object of corresponding
class, it need to be a static method.
Q) What is the order of execution of static methods?
Static methods are
executed in the order they are called, not in the order they are defined.
Q) Difference between instance
method and static method in java?
Instance
methods can be called by the object of a Class whereas static method are called
by the Class.
When objects
of a Class are created, they have their own copy of instance methods and
variables, stored in different memory locations.
Static
Methods and variables are shared among all the objects of the Class, stored in
one fixed location in memory.Static methods cannot access instance variables or instance methods
directly-they must use an object reference. Also, class methods cannot use the this keyword as there is no instance for
this to refer to.
Q) Static method can be overridden?
Yes, static methods can be
overridden just like any other methods.
Q) What does it mean when a method or
field is static?
A static
field or method is a keyword to indicate that:
A) For a
class member, the memory address of the member for all instances of this class
is shared.
B) For a
class method, an instance of this class is not needed to be called.
reference.methodname();
Examples:
// A class static method can be called in the same class without class name and without ref name
// A class static method can be called in the same class without class name and without ref name
public class Demo {
public static void
main(String[] args) {
fun();// with out class name and
without ref name
Demo.fun();// with class name
Demo
r1,r2;
r1=new Demo();
r2
= null;
r1.fun();// with ref name(ref
doesn't assign to null)
r2.fun();// with ref name but
ref assigned to null
}
static void fun()
{
System.out.println("I Am In Fun");
}
}
// main method can call only static methods
of the same class because main is also static method
class Sample {
void fun1(){
System.out.println("
I Am in Fun1");
}
static void fun2(){
System.out.println("
I Am in Fun2");
}
public static void main(String[]
args) {
//fun1();
it leads to compilation error
fun2();
}
}
Q) What is Non static
method?
The method which doesn’t have static keyword in its prototype is
called non static method. Like non static variables, non static methods also
should be called only by using object.
Q) How to call Non
static method?
reference.methodname();
or
new ClassName().methodname();
Example:
package NONSTATIC;
public class NonStaticDemo {
void myself()
{
System.out.println("I am In
Fun");
}
public static void main(String[]
args) {
// myself(); Not allowd
new
NonStaticDemo().myself();
NonStaticDemo s = new
NonStaticDemo();
s.myself();
}
}