What are static method? and how to use them?

0 votes
402 views
Anonymous User asked 13-Nov-2017 in programming by Anonymous User
edited 16-Jul-2018 by Harsh-Srivastava

What are static method? and how to use them?

1 Answer

0 votes
Arti Mishra answered 16-Jul-2018 by Arti Mishra
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();
    }
}