Home > DeveloperSection > Interviews > What is static method in java?

Posted on    March-26-2015 12:07 AM

 Java Java 
 2 Answer(s)
  559  View(s)
Rate this:

ben reitman
ben reitman

Total Post:6

Posted on    March-26-2015 12:13 AM

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) {
  static void display() {
    System.out.println("Java is my favorite programming language.");

Modified On Mar-15-2016 06:44:54 AM

Mayank Tripathi
Mayank Tripathi

Total Post:807

Posted on    March-26-2015 12:38 AM

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 s1 = new Student9 (111,"Karan");  
    Student9 s2 = new Student9 (222,"Aryan");  
    Student9 s3 = new Student9 (333,"Sonoo");  

111 Karan BBDIT        
222 Aryan BBDIT
333 Sonoo BBDIT

Don't want to miss updates? Please click the below button!

Follow MindStick