Methods in Java

Vinnu Mukhi
May 27, 2021

Example of Methods in Java:

public class Methods {
static int logic(int x, int y){
int z;
if(x>y){
z=x+y;

}
else{
z=(x+y)*5;
}
return z;
}
public static void main(String[] args) {
int a=2;
int b=1;
int c=logic (a,b);

int a1=7;
int b1=9;
int c1=logic (a1,b1);

int a2=2;
int b2=2;
int c2= logic(a2,b2);
System.out.println(c);
System.out.println(c1);
System.out.println(c2);
}
}
Output: 3
80
20

Method invocation using Object creation:

public class Methods {
int logic(int x, int y){
int z;
if(x>y){
z=x+y;

}
else{
z=(x+y)*5;
}
return z;
}
public static void main(String[] args) {
int a=2;
int b=1;
Methods obj= new Methods();
int c=obj.logic (a,b);

int a1=7;
int b1=9;
int c1=obj.logic (a1,b1);

System.out.println(c);
System.out.println(c1);
}
}
Output: 3
80

--

--

Vinnu Mukhi
0 Followers

I'm a Computer Science Student from Indore (India) and I'm Sharing all my learnings here.