📌 Chapter 07
⭐ 클래스(Class)
- 데이터(변수) + 기능(메소드)
- 틀

BankAccount myAccount1;
BankAccount myAccount2;
myAccount1 = new BankAccount();
myAccount2 = new BankAccount();
BankAccount myAccount3 = new BankAccount();
myAccount1.deposit(1000);
myAccount2.deposit(2000);
✅ 인스턴스 변수
- 클래스 내에 선언된 변수
- 같은 클래스 내에 위치한 메소드에서 접근가능
- 멤버변수, 필드 라고도 불림
✅ 인스턴스 메소드
✅ 참조변수의 특징
BankAccount Ho = new BankAccount();
Ho = new BankAccount();
BankAccount ref1 = new BankAccount();
BankAccount ref2 = ref1;
- 참조변수는 인스턴스를 참조한다.
- 참조변수는 인스턴스를 가리킨다.
✅ 참조변수의 매개변수 선언
class PassingRef {
public static void main(String[] args) {
BankAccount ref = new BankAccount();
ref.deposit(3000);
ref.withdraw(300);
check(ref);
}
public static void check(BankAccount acc) {
acc.checkMyBalance();
}
}
✅ 참조변수에 null 대입
BankAccount ref = new BankAccount();
ref = null;
BankAccount ho = null;
if(ho == null)
✅ class파일은 정의되는 클래스의 수만큼 생성된다.
⭐ 생성자(Constructor)
- 인스턴스의 멤버 초기화
- 생성자의 이름은 클래스의 이름과 동일해야 함
- 생성자는 값을 반환하지 않고 반환형도 표시하지 않는다
class BankAccount {
String accNumber;
String ssNumber;
int balance;
public BankAccount(String acc, String ss, int bal) {
accNumber = acc;
ssNumber = ss;
balance = bal;
}
}
class BankAccountConstructor {
public static void main(String[] args) {
BankAccount kim = new BankAccount("12-34-89", "990990-9090990", 10000);
BankAccount ho = new BankAccount("33-55-09", "770088-5959007", 10000);
}
}
- 인스턴스 생성의 마지막 단계는 생성자 호출이다
- 생성자 호출이 생략된 인스턴스는 인스턴스가 아니다
✅ 디폴트 생성자
class BankAccount{
int balance;
public BankAccount(){
}
}
- 직접 정의하지 않아도 자동으로 디폴트 생성자가 삽입된다.
⭐ 자바의 이름 규칙
✅ 클래스 이름 규칙
- 첫 문자는 대문자
- 둘 이상의 단어가 묶여서 하나의 단어를 이룰 때, 새로시작하는 단어는 대문자로 한다.
Circle + Point = CirclePoint
✅ 메소드, 변수 이름 규칙
- 첫 문자는 소문자
- 둘 이상의 단어가 묶여서 하나의 단어를 이룰 때, 새로시작하는 단어는 대문자로 한다.
Add + Your + Money = addYourMoney()
My + Age = myAge
✅ 상수 이름 규칙
- 모든 문자를 대문자
- 둘이상 단어 연결시에는 언더바로 구분
final int AGE = 30;
final int MY_AGE = 30;