Why do we create public static method in Java?

devquora
devquora

Posted On: Jan 12, 2023

 

In Java language, a public static method is a method that can be called without creating an instance of the class in which it is defined. Because these methods are not associated with any specific object, they can be invoked directly using the class name, without the need to create an instance of the class.

There are many reasons available, why we create public static methods in Java:

Utility methods: These are methods that perform some specific task and can be reused across multiple classes. Public static methods are useful for creating utility methods because they can be called from anywhere in the program without the need to create an instance of the class.

Factory methods: These are methods that are used to create and return objects of a particular class. Public static methods are useful for creating factory methods because they can be called directly using the class name.

Main method: The main method of a Java program is a public static method. This method is the entry point of a program and is executed when the program is run.

Performance: Accessing a static method is faster than accessing a non-static method because there is no need to create an instance of the class before calling the method.

    Related Questions

    Please Login or Register to leave a response.

    Related Questions

    Core Java Interview Questions

    Explain the significance of class loaders in Bootstrap?

    In order to load the Java Classes into a virtual environment, Classloader are used. A class loader will load key classe..

    Core Java Interview Questions

    What is the difference between JDK, JRE, and JVM?

    It is important to understand the difference between JDK, JRE, and JVM in Java.JVM (Java Virtual Machine) Java..

    Core Java Interview Questions

    What are the various access specifiers in Java?

    Access specifiers in java are the keywords which define the access scope of the function. It can be used before a class..