4
Reply

what's the difference between #static void method and void method?

Kamran Iqbal

Kamran Iqbal

8y
522
0
Reply

    If we use static void then it executes before main Otherwise after main

    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.

    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();}}

    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();}}