> For the complete documentation index, see [llms.txt](https://whs3-doge5k.gitbook.io/smartcontract-security-docs/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://whs3-doge5k.gitbook.io/smartcontract-security-docs/basics/2./undefined/undefined/visibility.md).

# 가시성 (Visibility)

Solidity 함수나 변수가 외부에서 어떻게 보이고 호출될 수 있는지를 결정하는 가시성(visibility)

**가시성은** 함수나 상태 변수에 붙는 `public`, `private`, `internal`, `external` 같은 키워드를 가시성(visibility)이라고 부른다. 이 키워드들은 "누가 이걸 사용할 수 있는지"를 정해준다. 예를 들어 어떤 함수가 **외부에서 쓸 수 있는지**, **오직 내부에서만 써야 하는지**를 정할 수 있다.

**`public`**

* **누구나 접근 가능**: 외부에서도, 컨트랙트 내부에서도 쓸 수 있다.
* 변수에 붙이면 **자동으로 getter 함수가 생성**된다.

```
uint public number = 1; // 자동으로 number() 함수 생성

function getNumber() public view returns (uint) {
    return number;
}

```

**`internal`**

* 이 컨트랙트 안에서 쓸 수 있고,\
  **상속한 컨트랙트**에서도 사용할 수 있다.

```solidity
solidity코드 복사uint internal data = 42;
```

> 라이브러리처럼 코드를 재사용할 때 유용하다.

**`external`**

* 외부에서만 호출할 수 있다.
* 내부에서 부르려면 `this.함수명()`처럼 `컨트랙트 주소`를 통해 호출해야 한다.
* 보통 **인터페이스에서 정의된 함수**가 여기에 해당한다.

```solidity
solidity코드 복사function externalCall() external view returns (uint) {
    return 100;
}
```

> 내부에서 `externalCall();` 하면 컴파일 에러\
> `this.externalCall();`처럼 사용해야 함

```solidity
// SPDX-License-Identifier: MIT
pragma solidity ^0.8.24;

contract VisibilityExample {
    string public name = "Solidity";
    uint private secret = 123;
    uint internal internalValue = 999;

    function getPublicName() public view returns (string memory) {
        return name;
    }

    function getSecret() public view returns (uint) {
        return secret;
    }

    function callExternal() external pure returns (string memory) {
        return "I'm external!";
    }

    function tryCallExternalInside() public view returns (string memory) {
        return this.callExternal(); // this. 붙이면 가능
    }
}

```
