static methods in Java can be called without creating an object of class. Have you noticed why we write static keyword when defining main it's because program execution begins from main and no object has been created yet.
ex,
class Languages {
public static void main(String[] args) {
display();
}
static void display() {
System.out.println("Java is my favorite programming language.");
}
}
Instance method requires an object of its class to be created before it can be called while static method doesn't require object creation.
ex.
class difference{
public static void main(String args[]){
display();
difference t = new difference();
t.show();
}
static void display(){
System.out.println("programming is amazing..");
}
void show(){
System.out.println("java is awesome");
}
}
Comments
Post a Comment