-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathMethodsInJava.java
More file actions
69 lines (54 loc) · 1.57 KB
/
MethodsInJava.java
File metadata and controls
69 lines (54 loc) · 1.57 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
package JavaSessions;
public class MethodsInJava {
//methods/functions : logic is written in a block
//you can call this block
//static / non static method
//can not create a method inside a method
//but can call the methods from diff methods
//duplicate methods are not allowed
//but we can overload the methods
//methods shoud be parallel to each other//independec to each other
//to call the non static mehtods -- we have to create the object of the class
//function can not return multiple values
//can return only one value at a time
public static void main(String[] args) {
MethodsInJava obj = new MethodsInJava();
obj.test();//calling a method
int c = obj.sum();
System.out.println(c);
String s = obj.getTrainerName();
System.out.println(s + " from Naveen Automation Labs");
String s1 = obj.getTrainerName();
if(s1.equals("Naveen")) {
System.out.println("PASS");
}
System.out.println(obj.getTrainerName());
int s2 = obj.add(10, 50);
System.out.println(s2);
}
//1. no input and no return:
//void --> does not return anything (no return keyword)
public void test() {
System.out.println("test method");
}
//2. no input but some return:
//return type: int
public int sum() {
System.out.println("sum method");
int a = 10;
int b = 20;
int z = a+b;
return z;
}
public String getTrainerName() {
System.out.println("get trainer name");
String name = "Naveen";
return name;
}
//3. some input and some return:
public int add(int a, int b) {
System.out.println("add method");
int sum = a+b;
return sum;
}
}