복습 : 애러가 나면 무슨 애러가 났는 지 알 수 있다. 예시)
The field Test01.n1 is not visible
=> 필드에 있는 데 불러올 수가 없어(접근 할 수 없어)
캡슐화 ==> 변수를 보낼 때 변형이 생겨서 오류가 나지 말게 하기 위해
변수를 감싸서 보내는 것
private는 함부로 건들 수 없다
그래서 public (메소드)중요! 로 간접적 접근을 하는 것(get,set 사용이유)
오늘의 코드
package edu.java.access08;
import java.util.Scanner;
// * Java Runtime Environment(JRE)
// - Java 애플리케이션을 생성하고 실행하기 위한 구성 요소
// - JRE는 Java 개발 도구(Java Development kit, JDK)의 일부
// - 운영체제와 Java 프로그램 사이에서 조정 역활을 수행
// * JVM(Java Virtual Machine)
// - Java 바이트 코드를 실행하는 주체(소프트웨어)
// - 특정 공각을 할당하여 메모리 관리
// JVM이 관리하는 메모리 영역 :
// - Stack(스택) : 지역 변수들이 저장되는 메모리 영역
// - Heap(힙) : 인스턴스(참조 자료형)가 저장되는 메모리 영역
// - Method(메소드) : static으로 선언한 변수, 메소드의 실행
// 코드들이 저장되는 메모리 영역
// * final 제한자
// - 초기화된 값을 바꿀 수 없음
// - final : 최종적인, 변경할 수 없는
// - final + 멤버 변수, 지역 변수 : 상수(한 번 초기화된 값을 변경할 수 없는 변수)
// - final + 메소드 : override 할 수 없는 메소드
// - final + 클래스 : 상속을 허용하지 않는 클래스
public class AccessMain08 {
public static final int MENU_INSERT = 1;
public static final int MENU_DELETE = 2;
public static int test = 100;
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "test";
// "test"는 힙 영역에 메모리를 차지
// str에는 주소값이 저장
str = "garbage"; // str에 "garbage"의 주소값이 저장
// "test"는 힙 영역에서 삭제되지 않음
// 이러한 객체를 쓰레기 값이라고 하며,
// 자바는 Garbage Collector를 이용하여 쓰레기 값을 자동으로 제거
// 멤버 변수 : 클래스 단계에서 선언된 변수
// 지역 변수 : 메소드나 특정 위치에 포함된 곳에서 선언된 변수
// - 지역 변수의 이름은 멤버 변수와 동일하게 선언되면 안됨
int x = 10;
test = 20; // 멤버 변수 초기화
//MENU_INSERT = 2; // final 변수는 초기화된 값을 변경할 수 없음
System.out.println("1. 등록 2. 삭제");
System.out.println("메뉴 선택>");
Scanner sc = new Scanner(System.in);
int select = sc.nextInt();
switch (select) {
case MENU_INSERT:
System.out.println("등록");
break;
case MENU_DELETE:
System.out.println("삭제");
break;
default:
break;
}
if(select == MENU_INSERT) {
System.out.println("추가 기능");
}
} // end main()
} // end AccessMain08
Contact 사용법!
package edu.java.contact01;
import java.util.*;
public class ContactMain01 {
public static final int MENU_QUIT = 0; // 종료
public static final int MENU_INSERT = 1; // 등록
public static final int MENU_SELECT_ALL = 2; // 전체검색
public static final int MENU_SELECT = 3; // 상세검색
public static final int MENU_UPDATE = 4; // 수정
public static final int MAX = 100; // 연락처 최대 저장 개수
// 연락처 저장 배열
public static Contact[] contactList = new Contact[MAX];
public static int count = 0; // 배열에 데이터를 저장하면 증가
public static Scanner sc = new Scanner(System.in);
public static void main(String[] args) {
String name, phone, email;
boolean run = true;
while (run) {
System.out.println("------------------------------------------------");
System.out.println("1. 등록 | 2. 전체검색 | 3. 상세검색 | 4. 수정 | 0. 종료");
System.out.println("------------------------------------------------");
System.out.println("선택>");
int result = sc.nextInt();
switch (result) {
case MENU_QUIT:
run = false;
break;
case MENU_INSERT:
System.out.println("-------------");
System.out.println("연락처 등록 메뉴");
System.out.println("-------------");
System.out.println("이름 입력>");
name = sc.next();
System.out.println("전화번호 입력>");
phone = sc.next();
System.out.println("이메일 입력>");
email = sc.next();
contactList[count] = new Contact(name, phone, email);
count++;
System.out.println("등록된 연락처 개수 : " + count);
System.out.println("연락처 등록 완료!");
break;
case MENU_SELECT_ALL:
System.out.println("연락처 개수 : " + count);
for (int i = 0; i < count; i++) {
System.out.println("--- 연락처 " + i + " ---");
System.out.println(contactList[i].toString());
}
break;
case MENU_SELECT:
System.out.println("-------------");
System.out.println("검색할 인덱스 입력>");
int i = sc.nextInt();
System.out.println(contactList[i].toString());
break;
case MENU_UPDATE:
System.out.println("-------------");
System.out.println("수정할 인덱스 입력>");
i = sc.nextInt();
System.out.println("이름 입력>");
name = sc.next();
System.out.println("전화번호 입력>");
phone = sc.next();
System.out.println("이메일 입력>");
email = sc.next();
contactList[i] = new Contact(name, phone, email);
System.out.println("연락처 수정 완료!");
break;
default:
break;
}
} // end while
} // end main();
} // end ContactMain01
package edu.java.contact01;
// 데이터 클래스(이름, 전화번호, 이메일)
public class Contact {
// 멤버 변수
private String name;
private String phone;
private String email;
// 기본 생성자
public Contact() {
// TODO Auto-generated constructor stub
}
// 매개변수 생성자
public Contact(String name, String phone, String email) {
this.name = name;
this.phone = phone;
this.email = email;
}
// getter/setter
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPhone() {
return phone;
}
public void setPhone(String phone) {
this.phone = phone;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
// toString()
@Override
public String toString() {
return "Contact [name=" + name + ", phone=" + phone + ", email=" + email + "]";
}
}