<instance method>
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
|
package ex01.method;
//instance method
//->객체 생성하고 사용해야함(메모리상에 할당이 되어야 사용가능)
class B{
int x,y;
public void setData(int xx, int yy) {
System.out.println(xx + "," + yy);
}
}//B class end
public class Ex03_InstanceMethod {
//메인
public static void main(String[] args) {
//객체(=다른 클래스에 들어갈 수 있는 key!) 생성, 메모리에할당됨, 생성자함수 자동호출
B b = new B();
b.setData(99, 3);
//객체는 여러개 생성가능(객체이름은 중복x)
B bb = new B();
bb.setData(10, 20);
//1.instance method 호출 방법-같은 class에 있어도 객체를 생성해서 호출해야함
//disp(); -> 그냥 부르면 안됨
Ex03_InstanceMethod im = new Ex03_InstanceMethod();
im.disp();
}
//===========1.instance 메소드============
public void disp() { //static이 없다..->main에서 객체 생성후 불러줘야 함
System.out.println("같은 class안에 있는 메소드");
}
}//Ex03_InstanceMethod class end
|
cs |
99,3
같은 class안에 있는 메소드
10,20
-객체 생성하고 사용해야함 (메모리상에 할당이 되어야 사용가능)
-instance method 호출 방법: 같은 class에 있어도 객체를 생성해서 호출해야함
<static method>
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
|
package ex01.method;
//instance method
//->객체 생성하고 사용해야함(메모리상에 할당이 되어야 사용가능)
class B{
int x,y;
public void setData(int xx, int yy) {
System.out.println(xx + "," + yy);
}
//================2.static method 객체 생성 없이 호출 가능=================
public static void show(String name) {
System.out.println(name);
}
}//B class end
public class Ex03_InstanceMethod {
public static void main(String[] args) {
//객체(=다른 클래스에 들어갈 수 있는 key!) 생성, 메모리에할당됨, 생성자함수 자동호출
B b = new B();
b.setData(99, 3);
//2.static method 호출 방법1 - 객체 생성 x (이걸로 쓰기)
B.show("syr111111");
//2.static method 호출 방법2 - 객체 생성 o (참고만)
b.show("syr222222");
//객체는 여러개 생성가능(객체이름은 중복x)
B bb = new B();
bb.setData(10, 20);
}
}
|
cs |
99,3
syr111111
syr222222
10,20
-객체 생성하고 사용해야함(메모리에 할당이 되어야 사용가능
▶static method 호출 방법 2가지
1)객체 생성 안하는 경우 -> 이걸로 쓰기!
2)객체 생성 하는 경우
'JAVA > 10_method' 카테고리의 다른 글
argument variable - 가변길이 인자 (0) | 2021.01.18 |
---|---|
static method - 객체 생성 없이 바로 사용 가능 (0) | 2021.01.18 |
char타입 리턴 (0) | 2021.01.18 |
return자료형과 매개변수 자료형의 크기에 따른 차이 (0) | 2021.01.18 |
return; 제어권 넘김 (0) | 2021.01.18 |