Static Method and Instance Method in Java

Java Programming Language / Class, Object and Methods in java

806

Program:

// static method and instance Method in java
class MethodExample {
	// this is a instance or non static method
	void sum()
	{
		System.out.println("Hello This is a non static Method");
	}
	
	// this is a static method
	static void add()
	{
		System.out.println("Hello This is a static Method");
	}
	
	
	public static void main(String args[])
	{
	// to access non static	or instance method we have create object
	// we can not access it directly
	MethodExample sahin = new MethodExample();
	sahin.sum(); // access through object
	
	// to access static method we can call it directly
	add();	// access without object
	}

}

Output:

Hello This is a non static Method
Hello This is a static Method

Explanation:

To access non static or instance method we have create object. We can not access it directly

	MethodExample sahin = new MethodExample();
	sahin.sum(); // access through object

To access static method we can call it directly

	add();	// access without object

This Particular section is dedicated to Programs only. If you want learn more about Java Programming Language. Then you can visit below links to get more depth on this subject.