Interviews4 answers
what's the difference between #static void method and void method?
4 Answers
If we use static void then it executes before main
Otherwise after main
Mukesh KumarPosted Aug 31, 2017, 5:40 AM EDT
static void method is a static method which does not return any thing. Static method can be called by directly by class name. void method is a method which also return nothing. But for calling simple method you have to create a class object and called method by object.method name.
sushil kumarPosted Jul 20, 2017, 10:01 AM EDT
1.> static void method is a static method that does not return anything. Static methods do not belong to any instance. They can only be called from the class directly.
Eg. :
public class MyClass
{
public static void myMethod()
{
System.out.println("My method");
}
}
public class MyAnotherClass
{
public void callMyMethod()
{
MyClass.myMethod();
}
}
The main method in a class is an example of a static void method. So the JVM process does not have to instantiate the class that contains the main method, to call the main method.
2.> While, void methods are instance methods inside a class that do not return anything. Unlike static methods, instance methods belong to instances of the class and not to the class directly. Eg. :
public class MyClass
{
public static void myMethod()
{
System.out.println("My method");
}
}
public class MyAnotherClass
{
public void callMyMethod()
{
MyClass myClassInstance = new MyClass();
myClassInstance.myMethod();
}
}
Ravi KumarPosted May 30, 2017, 3:42 AM EDT
1.> static void method is a static method that does not return anything. Static methods do not belong to any instance. They can only be called from the class directly.
Eg. :
public class MyClass
{
public static void myMethod()
{
System.out.println("My method");
}
}
public class MyAnotherClass
{
public void callMyMethod()
{
MyClass.myMethod();
}
}
The main method in a class is an example of a static void method. So the JVM process does not have to instantiate the class that contains the main method, to call the main method.
2.> While, void methods are instance methods inside a class that do not return anything. Unlike static methods, instance methods belong to instances of the class and not to the class directly. Eg. :
public class MyClass
{
public static void myMethod()
{
System.out.println("My method");
}
}
public class MyAnotherClass
{
public void callMyMethod()
{
MyClass myClassInstance = new MyClass();
myClassInstance.myMethod();
}
}
Ravi KumarPosted May 30, 2017, 3:42 AM EDT


