Methods in Java and Types of Methods in Java with Examples

Methods in Java: 

Method is a sub-block of a class that is used for implementing the logic of an object operation.

Syntax:

Modifier ReturnType NameOfMethod(Parameter List)
{
 // Body of Method//
}

Java Methods Example:


Methods in Java program:
class MethodExample
{
 public void funB()
 {
  System.out.println("FunB() of MethodExample.");
 }
 public static void main(String[] args)
 {
  MethodExample e1 = new MethodExample();
  e1.funB();
 }

}
Methods in Java program Output:
FunB() of MethodExample.

Types of Methods in Java: In Java methods are divided into three types. They are:

1) Based on the static modifier we have two types. They are.
  •  Static method.
  • Non-static method.
2) Based on return type we have two types. They are.
  • void method.
  • Non-void method.
3) Based on the parameter we have two types. They are.
  • Parameterized method.
  • Non-parameterized method.
Static Method: Method which is declared as static is called a Static method. JVM doesn’t execute a static method by itself. Static methods are executed if they are explicitly called by the user (or) developer either from the main method (or) from the static block. 

Methods in Java Program using Static method:

Java Static Method program:
class StaticMethodExample
{
 static void funB()
 {
  System.out.println("FunB() of StaticMethodExample");
 }
 static void funC()
 {
  System.out.println("FunC() of StaticMethodExample");
 }
 public static void main(String[] args)
 {
  funC();
  funB();
 }

}
Java Static Method program Output:
FunC() of StaticMethodExample.

FunB() of StaticMethodExample.

Note:
1)    To call the static method from another static method we don’t need an object of the class.
2)    Static methods are executed in the order they are called not in the order they are defined.

Non-static method: The method which does not have the static keyword in its prototype is called a Non-static method. In order to call non-method, we must use an object to call a method. JVM doesn’t execute the non-static method automatically we call them explicitly using the object.

Methods in Java Program using Non- static method:

Java Non-Static method program:
class NonStaticMethodExample
{
 void funB()
 {
  System.out.println("FunB() of Non-StaticMethodExample");
 }
 void funC()
 {
  System.out.println("FunC() of Non-StaticMethodExample");
 }
 public static void main(String[] args)
 {
  NonStaticMethodExample n1 = new NonStaticMethodExample();
  n1.funC();
  n1.funB();
 }

}
Java Non-static method program Output:
FunC() of Non-StaticMethodExample.

FunB() of Non-StaticMethodExample.
Note:
1)    Non-Static methods are executed in the order they are called not in the order they are defined.

Read Also: Java if Else Statement  Java For loop


Void method: A method which is declared as void is called a Void method.

Methods in Java Program using void method:

Java void method program:
class VoidMethodExample
{
 void funB()
 {
  System.out.println("FunB() of voidMethodExample");
 }
 void funC()
 {
  System.out.println("FunC() of voidMethodExample");
 }
 public static void main(String[] args)
 {
  VoidMethodExample v1 = new VoidMethodExample();
  v1.funC();
  v1.funB();
 }

}
Java void method program Output:
FunC() of voidMethodExample.
FunB() of voidMethodExample.
Non-void method: Method which is declared using primitive data types (int, float, byte,etc..) is called Non-void method.

Methods in Java Program using Non-void method:

Java Non-void method program:
class Non-voidExample
{
 int getSq(int x)
 {
 System.out.println(“ Non-void method Example”);
  int sq = x * x;
  return(sq);
 }

 public static void main(String[] args)
 {
  Non-voidExample d1 = new Non-voidExample ();
  int x = d1.getSq(2);
  System.out.println("X = " +x);
  System.out.println(d1.getSq(4));
  int y = d1.getSq(5) + 2;
   System.out.println("Y = " +y);
 }

}
Java Non-void method program output:
Non-void method Example.
x = 4
16 
Parameterized method: Methods which are declared with parameters using void (or) any primitive data types are called Parameterized method.

Methods in Java Program Parameterized method:

Java Parameterized method program:
class ParameterExample
{
void funP(int i, int j)
{
 i = i + 1;
 j = j + 1;
System.out.println("Tutorialsweb Parameter Example in Java " );
System.out.println(" i = " + i + " j = " + j );
}
public static void main(String[] args)
{
 ParameterExample p1 = new ParameterExample();
 p1.funP(2,4);
}

}
Java Parameterized program output:
Tutorialsweb ParameterExample in Java.
 i = 3 j = 5
Non-parameterized method: Methods which are declared without parameters is called Non-parameterized method.

Methods in Java Program using Non-parameterized method:

Java Non-parameterized method program:
class NonParameterExample
{
 int i,j;
void funP()
{
 i = i + 1;
 j = j + 1;
System.out.println("Tutorialsweb NonParameterExample in Java " );
System.out.println(" i = " + i + " j = " + j );
}
public static void main(String[] args)
{
 NonParameterExample p1 = new NonParameterExample();
 p1.funP();
}

}
Java Non-parameterized program output:
Tutorialsweb NonParameterExample in Java

 i = 1 j = 1
Share:

Ads

Search This Blog

  • ()
Powered by Blogger.

Strings in C With Syntax, C String Program with output

Strings in C :  Strings can be defined as the one-dimensional array of characters terminated by a null ('\0'). The difference betwee...

Blog Archive