What are static method? and how to use them?

Asked 13-Nov-2017
Updated 16-Jul-2018
Viewed 687 times

1 Answer


0

Static Method:
Static methods are those methods that are defined using static keyword. If you define any method as static you don’t need to create an object of that particular method. And a static method call directly by using the class name.

How to use static method :
class_name.static_method_name();

Example –
class Student{
     int rollno;
     String name;
     static void dispaly(){
     System.out.println(“Static block Call”);
     }
     Student(int r, String n){
     rollno = r;
     name = n;
     }
     void displayData()
{
System.out.println(rollno+" "+name);
}

    public static void main(String args[])
{
    Student.display();
    Student s1 = new Student9 (111,"Arti");
    Student s2 = new Student9 (222,"Harsh");
    Student s3 = new Student9 (333,"Aditya");
   Student s4 = new Student9 (333,"Vishal");
    s1.display();
    s2.display();
    s3.display();
   s4.display();
    }
}