class Methods
{
// Constructor method
Methods()
{
System.out.println("Constructor method is called when an object of it's class is created");
}
// Main method where program execution begins
public static void main(String[] args)
{
staticMethod();
Methods object = new Methods();
object.nonStaticMethod();
}
// Static method
static void staticMethod()
{
System.out.println("Static method can be called without creating object");
}
// Non-static method
void nonStaticMethod()
{
System.out.println("Non static method must be called by creating an object");
}
}
Step-by-Step Explanation:
1. Class Declaration:
class Methods {
‘Class Methods’ is declared with the existence of a constructor, a static method, non-static method, and the main method.
2. Constructor Method:
Methods()
System.out.println(“Constructor method is called when an object of it’s class is created”);
}
- The above is a constructor method. A constructor in Java gets automatically called when an object of the class is created.
- A constructor has the same name as the class (Methods), and its return type is missing.
- Constructor prints the message: “Constructor method is called when an object of its class is created” when an object of the Methods class is being created.
3. Main Method (Program Execution Starts Here):
public static void main(String[] args)
{
staticMethod();
Methods object = new Methods();
object.nonStaticMethod();
}
The primary approach is the main method where the program begins execution. This is the starting point of any Java application.
Inside the main method:
- The static method staticMethod() is invoked.
- An object of the class Methods is instantiated by using the operator new Methods(). It invokes the constructor, which displays a message.
- The non-static method nonStaticMethod() is invoked on the newly instantiated object object.
4. Static Method:
static void staticMethod()
{
System.out.println(“Static method can be called without creating object”);
}
- Here, a static method is declared. A static method is associated with the class rather than with an instance of the class.
- This method does not require creating an object of the class to call it. It is invoked directly in the main method through staticMethod().
5. Non-Static Method:
void nonStaticMethod()
{
System.out.println(“Non static method must be called by creating an object”);
}
- Here, a non-static method is declared. Non-static methods are the methods of an object, thus they need to be called through an object of the class.
- The created object object.nonStaticMethod() is used to call the non-static method in the main method.
Program Execution Flow:
1. Calling staticMethod():
- The main method calls the static method first.
- So, this will print “Static method can be called without creating object”.
2. Creating the Object (Methods object = new Methods()):
- Whenever a new object of Methods is created, the constructor is invoked.
- The constructor prints: “Constructor method is called when an object of its class is created”.
3. Calling nonStaticMethod():
- Once the object is created, the non-static method nonStaticMethod() is invoked on the object.
- This prints: “Non static method must be called by creating an object”.
Program Output:
The program output will be as follows:
Static method can be called without creating object
Constructor method is called when an object of it’s class is created
Non static method must be called by creating an object
Summary:
- Static method: This can be called directly using the method name without needing an object.
- Non-static method: Requires an object to be created before it can be called.
- Constructor: Automatically called when an object is created, and it initializes the object.
- Program flow: The static method is called first, followed by the constructor when an object is created, and then the non-static method is called.