class Difference {
public static void main(String[] args) {
display(); // calling without object
Difference t = new Difference();
t.show(); // calling using object
}
static void display() {
System.out.println("Programming is amazing.");
}
void show() {
System.out.println("Java is awesome.");
}
}
Step-by-Step Explanation:
1. Class Declaration:
- The class Difference consists of the following two methods: display() is a static method, and show() is a non-static method.
- The method is main is the entry of the program.
2. Static Method Call (display())
display(); // calling without object
- A static method can be called directly from the main method without creating an object.
- The display() method is static, thus it is called directly because no instance (object) of the class has to be created.
- When the display() method is called, it prints the following message:
Programming is amazing.
3. Non-Static Method Call (show()):
Difference t = new Difference();
t.show(); // calling using object
- Non-static methods call for an object to be invoked.
- In this example, an object of the class Difference is created by using the keyword new Difference(). This object is addressed through the variable t.
- The method show() is called with the object t. This outputs the message:
Java is awesome.
4. Method Declarations:
static void display()
System.out.println(“Programming is amazing.”);
}
- The display() method is static, in other words, it is a property of the class itself and not of an instance of the class.
- It prints “Programming is amazing.”.
void show() {
System.out.println(“Java is awesome.”);
}
- The show() method is instance – it is a method of an instance of the class.
- It outputs to the console “Java is awesome.”.
Program Execution Flow
1. A call to the main method occurs.
2. Direct call since the display() is a static method.
- It prints the statement to console: “Programming is amazing.”
3. Declaration of an instance of the class Difference (Difference t = new Difference(); )
4. use an object called t to make the call: t.show().
It displays: Java is awesome.
- Static method (display() ): It is called without declaring an instance of the class. It is from the class.
- Non-static method (show() ): This requires an object of the class to be invoked. It belongs to an instance of the class.