WHAT IS STATIC METHOD IN JAVA?

ben reitman

Total Post:6

Points:30
Posted by  ben reitman
Java 
 806  View(s)
Ratings:
Rate this:
  1. ben reitman

    Post:6

    Points:30
    What is static method in java?

    The static keyword in java is used for memory management mainly. We can apply java static keyword with variables, methods, blocks and nested class. The static keyword belongs to the class than instance of the class. java static methods  can be called without creating an object of class. Have we noticed why we write static keyword when defining main it's because program execution begins from main and no object has been created yet. Consider the example below of static methods.

    class programming {
      public static void main(String[] args) {
        display();
      }
     
      static void display() {
        System.out.println("Java is my favorite programming language.");
      }
    }

      Modified On Mar-15-2016 06:44:54 AM
  1. Mayank Tripathi

    Post:807

    Points:4035
    What is static method in java?

    If you apply static keyword with any method, it is known as static method.

    • A static method belongs to the class rather than object of a class.
    • A static method can be invoked without the need for creating an instance of a class.
    • static method can access static data member and can change the value of it.

    //Program of changing the common property of all objects(static field).  
      
    class Student9{  
         int rollno;  
         String name;  
         static String college = "ITS";  
           
         static void change(){  
         college = "BBDIT";  
         }  
      
         Student9(int r, String n){  
         rollno = r;  
         name = n;  
         }  
      
         void display (){System.out.println(rollno+" "+name+" "+college);}  
      
        public static void main(String args[]){  
        Student9.change();  
      
        Student9 s1 = new Student9 (111,"Karan");  
        Student9 s2 = new Student9 (222,"Aryan");  
        Student9 s3 = new Student9 (333,"Sonoo");  
      
        s1.display();  
        s2.display();  
        s3.display();  
        }  
    }  

    Output:
    111 Karan BBDIT        
    222 Aryan BBDIT
    333 Sonoo BBDIT

Answer

NEWSLETTER

Enter your email address here always to be updated. We promise not to spam!