-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmethods.java
More file actions
38 lines (35 loc) · 1.24 KB
/
methods.java
File metadata and controls
38 lines (35 loc) · 1.24 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
public class methods {
/*static int logic (int x, int y){ //static is used to call the method without creating an object of the class
int z;
if (x>y){
z = x+y;
}
else {
z = (x+y)*5;
}
return z;
}
public static void main(String[] args) {
int a = 5;
int b = 10;
int c = logic(a,b);
System.out.println(c);
}*/
//calling a method is done by using the method name followed by parentheses. If the method has parameters, you need to pass the appropriate arguments within the parentheses. In this example, we call the `logic` method with `a` and `b` as arguments, and it returns the result which is stored in variable `c`. Finally, we print the value of `c`.
int logic (int x, int y){ //non-static method requires an object to call it
int z;
if (x>y){
z = x+y;
}
else {
z = (x+y)*5;
}
return z;
}
public static void main(String[] args) {
methods obj = new methods(); //creating an object of the class to call the non-static method
int c;
c = obj.logic(5,10); //calling the non-static method using the object
System.out.println(c);
}
}