public class StaticTest {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = StaticTest.hap(a,b);
System.out.println(sum);
}
public static int hap(int a, int b) {
int v = a+b;
return v;
}
}
메인(시작)클래스가 동작(실행)되는 방식
정해진 메모리(static-zone)
위치에 한번 자동으로 로딩한다.main()
메서드가 static
이기 때문에 메모리에 자동으로 로딩한다.Stack Area
)에 push
(기계어코드를 넣고) 한 뒤 동작을 시작한다.Call Static Fame Area
(Stack Area)
LIFO
(Last-In-First-Out) 구조이다.PC
프로그램 종료
public class NoneStaticTest {
public static void main(String[] args) {
int a = 10;
int b = 20;
NoneStaticTest st = new NoneStatictest();
int sum = st.hap(a,b);
System.out.println(sum);
}
public int hap(int a, int b) {
int v = a+b;
return v;
}
}
Method Area
Heap Area
클래스명.호출메서드 (ex. MyUtil.hap)
public class StaticAccess {
public static void main(String[] args) {
int a = 10;
int b = 20;
int sum = MyUtil.hap(a,b);
System.out.println(sum);
}
}
public class MyUtil {
public static int hap(int a, int b) { // 클래스 메서드
int v = a+b;
return v;
}
}
static 멤버는 클래스를 사용하는 시점에서 자동으로 static-zone에 로딩된다.
따라서 new를 이용해서 객체를 생성할 필요가 없다.
public class NoneStaticAccess {
public static void main(String[] args) {
int a = 10;
int b = 20;
// MyUtil1
// 객체 생성
MyUtil1 my1 = new MyUtil1();
int sum = my1.hap(a,b);
System.out.println("sum = " + sum);
}
}
public class MyUtil1 {
public int hap(int a, int b) {
int v = a+b;
return v;
}
}
public class AllStaticTest {
public static void main(String[] args) {
int a = 10;
int b = 20;
// AllStatic st = new AllStatic();
// System.out.println(st.hap(a,b));
// System.out.println(st.max(a,b));
// System.out.println(st.min(a,b));
System.out.println(AllStatic.hap(a,b));
System.out.println(AllStatic.max(a,b));
System.out.println(AllStatic.min(a,b));
}
}
public class AllStatic {
private AllStatic() { // private 생성자 생성
}
public static int hap(int a, int b) { // 총합
int v = a+b;
return v;
}
public static int max(int a, int b) { // 최대값
return a > b ? a : b;
}
public static int min(int a, int b) { // 최소값
return a < b ? a : b;
}
}
class
의 이름으로 static
멤버들을 호출하기 위해
private
생성자를 만들어 객체생성을 하지 못하도록 막는다.
Class
(클래스) : 객체를 모델링하는 도구(설계도)
Object
(객체) : class를 통해서 선언되는 변수
Instance
(인스턴스, 실체) : 객체생성에 의해 메모리(Heap Memory)에 만들어진 객체
-> Class
와 Object
는 서로 비슷한 개념(객체를 나타내는 용어)
객체를 모델링하는 도구(설계도)
public class Student {
private String name;
private String dapt;
private int age;
private String email;
private int year;
private String phone;
public Student() {
}
// 이하 생략
}
class를 통해서 선언되는 변수
Student st;
객체생성에 의해 메모리(Heap Memory)에 만들어진 객체
st = new Student();