-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path230306.html
More file actions
66 lines (61 loc) · 4.25 KB
/
230306.html
File metadata and controls
66 lines (61 loc) · 4.25 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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta http-equiv="X-UA-Compatible" content="IE=edge">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>객체지향언어</title>
<title>클래스 메서드와 인스턴스 메서드</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/styles/default.min.css">
<link href="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/css/bootstrap.min.css" rel="stylesheet" integrity="sha384-GLhlTQ8iRABdZLl6O3oVMWSktQOp6b7In1Zl3/Jr59b6EGGoI1aFkw7cmDA6j6gD" crossorigin="anonymous">
<script src="https://cdn.jsdelivr.net/npm/bootstrap@5.3.0-alpha1/dist/js/bootstrap.bundle.min.js" integrity="sha384-w76AqPfDkMBDXo30jS1Sgez6pr3x5MlQ1ZAGC+nuZB+EYdgRZgiwxhTBTkF7CXvN" crossorigin="anonymous"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/highlight.js/11.7.0/highlight.min.js"></script>
<link href="./static/css/main.css" rel="stylesheet">
</head>
<body>
<div class="md-container">
<h2 class="fw-bold">기본형 매개변수 vs 참조형 매개변수</h2>
<p>자바에서는 메서드를 호출할 때 매개변수로 지정한 값을 메서드의 매개변수에 복사해서 넘겨준다.</p>
<p>기본형의 경우 기본형 값이 복사되지만 참조형의 경우 인스턴스의 주소값이 복사된다.</p>
<p>따라서 기본형 매개변수의 경우 읽기만 할 수 있지만 참조형의 경우 값을 일고 변경 또한 가능하다.</p>
<pre><code class="lang-java"><span class="hljs-keyword">Class</span> <span class="hljs-keyword">Data</span>{
<span class="hljs-built_in">int</span> x;
<span class="hljs-keyword">public</span> <span class="hljs-keyword">Data</span>(<span class="hljs-built_in">int</span> x) {
this.x = x;
}
}
<span class="hljs-keyword">public</span> <span class="hljs-keyword">Class</span> Main {
<span class="hljs-keyword">public</span> static void main(String args[]) {
<span class="hljs-keyword">Data</span> <span class="hljs-keyword">data</span> = new <span class="hljs-keyword">Data</span>(<span class="hljs-number">10</span>);
change(<span class="hljs-keyword">data</span>.x);
System.<span class="hljs-keyword">out</span>.println(<span class="hljs-keyword">data</span>.x);
change(<span class="hljs-keyword">data</span>);
System.<span class="hljs-keyword">out</span>.println(<span class="hljs-keyword">data</span>.x);
}
static change(<span class="hljs-keyword">Data</span> <span class="hljs-keyword">data</span>) {
<span class="hljs-keyword">data</span>.x = <span class="hljs-number">100</span>;
}
static change(<span class="hljs-built_in">int</span> x) {
x = <span class="hljs-number">100</span>;
}
}
</code></pre>
<p>위의 예제의 경우 매개변수를 기본형 int로 한 메서드를 실행시켜도 인스턴스의 멤버변수 x의 값은 변경되지 않고 매개변수를 참조형 Data로 한 메서드를 실행시킨 후에는 인스턴스의 멤버변수 x의 값이 변경되는 것을 확인할 수 있을 것이다.</p>
<h3 id="-">참조형의 반환 타입</h3>
<p>반환 타입이 참조형인 경우 매개변수와 같이 인스턴스의 주소값이 반환되는 것이다.</p>
<h3 id="-">클래스 메서드와 인스턴스 메서드</h3>
<p>인스턴스 메서드는 인스턴스 변수와 관련된 작업을 하는, 즉 메서드의 작업을 수행하는 데 인스턴스 변수를 필요로 하는 메서드이다.</p>
<p>이를 달리 말하면 인스턴스와 관계없는 메서드를 클래스 메서드로 정의한다는 것을 의미하고 static 메서드의 경우 인스턴스 변수나 메서드가 호출되어서는 안된다.</p>
<ol>
<li><p>클래스를 설계할 때 멤버변수 중 모든 인스턴스에 공통으로 사용하는 것에 static을 붙인다.</p>
</li>
<li><p>클래스 변수는 인스턴스를 생성하지 않아도 사용할 수 있다.</p>
</li>
<li><p>클래스 메서드는 인스턴스 변수를 사용할 수 없다.(인스턴스가 생성되었는 지를 보장할 수 없기 때문)</p>
</li>
<li><p>메서드 내에서 인스턴스 변수를 사용하지 않는다면 static을 붙이는 것을 고려한다.</p>
</li>
</ol>
</div>
</body>
</html>