카테고리 없음
[java] 프로젝트를 들어가기 전 - 자바 네이밍 규칙
juno0432
2024. 8. 1. 14:56
규칙 관례
- 패키지
항상 소문자
myapppackage myapp; // 올바른 패키지 이름 예시
- 클래스, 인터페이스 이름
파스칼 표기법 : 각 단어의 첫 글자를 대문자로 하여 작성
Customer, AcoountManager
public class Customer { // 클래스 내용 } public interface AccountManager { // 인터페이스 내용 }
- 메서드 이름
카멜 표기법 : 메서드 첫 단어는 소문자로 그다음 단어의 첫 글자는 대문자로
calculateTotal()
public class Order { public void calculateTotal() { // 메서드 내용 } }
- 변수 이름
카멜 표기법 : 메서드 첫 단어는 소문자로 그다음 단어의 첫 글자는 대문자로
String userName, int number
public class User { private String userName; private int number; public void setUserName(String userName) { this.userName = userName; } public void setNumber(int number) { this.number = number; } }
- 상수 이름
대문자 : 모든 단어를 대문자로 사용하며 단어 사이에 언더바(_)를 작성한다
MAX_VALUE, PI
public class MathConstants { public static final double PI = 3.14159; public static final int MAX_VALUE = 100; }
규칙2
아래는 자바에서 특정한 의미를 가지는 예약어로 변수명이나, 클래스명등으로 사용 할 수 없다.
abstract | assert | boolean | break | byte | case | catch |
char | class | const | continue | default | do | double |
else | extends | false | final | finally | float | for |
goto | if | implements | import | instanceof | int | interface |
long | native | new | null | package | private | protected |
public | return | short | static | super | switch | synchronized |
this | throw | throws | transient | true | try | void |
volatile | while |
규칙3
변수명과 클래스명을 보고 어떤 작업을 하는가 추론이 가능해야함