Methods In Java

Java Methods are callable pieces of code which contain some logic to perform an operation and when invoked, may or may not return a value. Java support 6 types of method.

  1. Static Method
  2. Non-static Method
  3. Final Method
  4. Native Method
  5. Abstract Method
  6. Synchronized Method

Static Method

  • If the method is declared with a static keyword then it is known as a static method.
  • A static method is not a part of java object.
  • Static methods can be called directly, by class name or object name.

Example:

public class Demo {
	static void display(int a, int b){
		System.out.println("In display method");
		int add=a+b;
		System.out.println("The result is "+add);
	}
public static void main(String[] args) {
display(6,5); //calling directly
Demo.display(9, 4); //calling using class name
Demo obj1=new Demo();
obj1.display(4, 5); // calling using object name
}
}

Non-Static Method

When a method is not declared with a static keyword then it is known as a non-static method. These methods can only be called by object name.

Example:

public class Demo {
	 void display(int a, int b){
		System.out.println("In display method");
		int add=a+b;
		System.out.println("The result is "+add);
	}
public static void main(String[] args) {

Demo obj1=new Demo();
obj1.display(4, 5); // calling using object name
}
}

 

Elaborating Illustration on Static and Non-Static Methods

java methods

Example 1:

public class Test {
	
	static void display(){
		System.out.println("In static display method");
	}
	void show(){
		System.out.println("In non-static display method");
	}
	static{
		display(); //call static methods directly
		new Test().show(); //call by object name as show() is non static
	}
	public static void main(String[] args) {
		System.out.println("static main method");
	}

}

//OUTPUT
/*In static display method
In non-static display method
static main method
*/

Example 2:

public class Test {

	static void display() {
		System.out.println("In static display method");
	}

	void show() {
		System.out.println("In non-static display method");
		display();
		func();
	}

	void func() {
		System.out.println("Non static method func");
	}

	public static void main(String[] args) {
		System.out.println("static main method");
		display(); // called directly as static
		new Test().func(); // called by object name as non static
		new Test().show(); // called by object name as non static

	}

}

//OUTPUT
/*
static main method
In static display method
Non static method func
In non-static display method
In static display method
Non static method func*/

Final Method

Final is a keyword in Java which can be used for:

  1. Variables
  2. Methods
  3. Class
  • If a variable is final the value of the variable cannot change once assigned.

Example:

public class Test {
	static final int x=10;
	public static void main(String[] args) {
		System.out.println(x);
		x=x+1; //ERROR, cannot assign a value to final variable x.
	}
	

}
  • If the logic of the method is fixed then that method can be declared as a final method.

Did you know?

A Final method cannot be overridden in Java.

Example:

public class Test {
	final void show(){
		System.out.println("In show method of Test class");
	}
}
public class Demo extends Test {
	 void show(){
		 //ERROR, cannot override final method
	 }
}
  • If a class is declared final then it cannot be inherited ( a process by which a child class accesses the resources of a parent class).
public final class Test {
	public void show(){
		System.out.println("In show method of Test class");
	}
	
}
public class Demo extends Test {
	 //ERROR, cannot inherit from final class
}

Abstract Method

  • In Java, if a method is declared with the abstract keyword then it is known as an abstract method.
  • The abstract method supports the concept of abstraction.
  • Abstraction in Java is possible in 2 ways:-
  1. By Abstract class
  2. By Interface
  • An abstract is a keyword in Java which is used for an abstract method or abstract class. If a method is abstract it cannot have a body/ method definition.
  • If any method of a class is abstract then the corresponding class should be declared as an abstract class.
  • An abstract class may or may not contain any abstract method.
  • If a class is abstract then it cannot be instantiated.

Note: Method with a body is a complete method and without a body is an abstract method.

Example:



//abstract class
abstract class Test {
	 void show(){
		System.out.println("In show method of Test class");
	}
	 abstract void display(); //abstract method
	 public static void main(String[] args) {
		System.out.println("In main method");
		Test obj1=new Test(); //ERROR, cannot instantiate an abstract class
	}
	
}

Native Method

A native method is a Java method whose implementation is done in another programming language like C. This method indicates to the compiler that the existence of method should not be checked at the time of compilation but always check at the time of execution. native methods do not contain a body in Java as they are implemented in another programming language.

Example:

public class Test {
	native void show(); //method will be checked during execution
	
	 public static void main(String[] args) {
		System.out.println("In main method");
		Test obj1=new Test();
		obj1.show();
	}
	
}