A static method is a method that can be known and can be executed without creating an object . They are also used to create instance method.
Static method can also be invoked directly to the via class name i.e., we don't have to create an object for a class in order to initiate the static method.
In Java Program we write "main()" method which is Static method which is directly known by JVM. We create objects inside the "main()" method so no object can be create before execution of "main()" method. Therefore the "main()" method is known as Static method.
Instance Method
An instance method is a type of method which requires an object of the class within which it is defined to be created before it can be known. If the method in Java is not clearly it can be defined as static it is an instance method.
These methods can be known within the same class in which they reside or from different classes defined either in same packages or other packages depending on the access type provided to the desired instance method.
AN EXAMPLE
// example to accessing an instance method
import.java.io*;
class Fruits
{
String name= "";
public static void main(String[] args)
{
Fruits ob = new Fruits();
ob.apple("Green apple");
System.out.println(ob.name);
}
}
In the above example, we had created two classes namely, Fruits and starts. The Fruits class contains an instance method named apple and starts class contain the main method to start the execution of the program. The apple method receives a parameter of the string type and we have set the name of parameter as "name" itself. And the data type of that parameter is string.
Subscribe My YouTube Channel:- CLICK HERE
Post a Comment