0% found this document useful (0 votes)
18 views4 pages

Possible Combinations of Method

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views4 pages

Possible Combinations of Method

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as TXT, PDF, TXT or read online on Scribd
You are on page 1/ 4

1) Method will not take parameter/argument and will not return anything.

Example :

package method;

public class MethodCombinations {

public void add() {


int a = 20;
int b = 80;
int c = a + b;
System.out.println(c);
}

public static void main(String[] args) {

MethodCombinations m = new MethodCombinations();


m.add();
}
}

Example 2:

package method;

import java.util.Scanner;

public class MethodCombinations {

public void add() {


Scanner sc = new Scanner(System.in);
System.out.println("Enter first Number");
int no1 = sc.nextInt();
System.out.println("Enter Second Number");
int no2 = sc.nextInt();

int c = no1 + no2;


System.out.println(c);
}

public static void main(String[] args) {

MethodCombinations m = new MethodCombinations();


m.add();

}
}

2) Method will take parameter/argument and will return result.

Example :

package method;
public class MethodCombinations {

public int add(int a, int b) {

int c = a + b;
return c;
}

public static void main(String[] args) {

MethodCombinations m = new MethodCombinations();


int result = m.add(10, 20);
System.out.println(result);
}
}

Example :

package method;

import java.util.Scanner;

public class MethodCombinations {

public int add(int no1, int no2) {

int c = no1 + no2;


return c;
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter first Number");
int no1 = sc.nextInt();
System.out.println("Enter Second Number");
int no2 = sc.nextInt();

MethodCombinations m = new MethodCombinations();


int result = m.add(no1, no2);
System.out.println(result);

}
}

3) Method will take parameter/argument and will not return result.

Example :

package method;

public class MethodCombinations {

public void add(int a, int b) {

int c = a + b;
System.out.println(c);
}

public static void main(String[] args) {

MethodCombinations m = new MethodCombinations();


m.add(10, 20);

}
}

Example :

package method;

import java.util.Scanner;

public class MethodCombinations {

public void add(int no1, int no2) {

int c = no1 + no2;


System.out.println(c);
}

public static void main(String[] args) {


Scanner sc = new Scanner(System.in);
System.out.println("Enter first Number");
int no1 = sc.nextInt();
System.out.println("Enter Second Number");
int no2 = sc.nextInt();

MethodCombinations m = new MethodCombinations();


m.add(no1, no2);

}
}

4) Method will not take parameter/argument and will return result.

Example :

package method;

public class MethodCombinations {

public int add() {


int a = 90;
int b = 45;
int c = a + b;
return c;
}

public static void main(String[] args) {

MethodCombinations m = new MethodCombinations();


int result = m.add();
System.out.println(result);

}
}

Example :

package method;

import java.util.Scanner;

public class MethodCombinations {

public int add() {


Scanner sc = new Scanner(System.in);
System.out.println("Enter first Number");
int no1 = sc.nextInt();
System.out.println("Enter Second Number");
int no2 = sc.nextInt();

int c = no1 + no2;


return c;
}

public static void main(String[] args) {

MethodCombinations m = new MethodCombinations();


int result = m.add();
System.out.println(result);

}
}

You might also like