Notice
Recent Posts
Recent Comments
Link
«   2024/09   »
1 2 3 4 5 6 7
8 9 10 11 12 13 14
15 16 17 18 19 20 21
22 23 24 25 26 27 28
29 30
Tags
more
Archives
Today
Total
관리 메뉴

개발 일기

JAVA 코딩 연습(static 제한자를 활용) 본문

JAVA 연습장

JAVA 코딩 연습(static 제한자를 활용)

개발 일기 2021. 6. 9. 22:07

//Static 클래스 생성

public class Static {
public int instance_field=100;
public static int static_field=200;

public void instance_method() { // 객체(instance) 메소드 생성
    System.out.println("난 인스턴스 (객체)메쏘드");
}
public static void static_method() { // 정적(static) 메소드 생성
    System.out.println("난 정적(static)메쏘드");
}

}

//StaticMain 클래스 생성

public class StaticMain {

public static void main(String[] args) {
    System.out.println("--------static member access--------");
    /*
     * 정적멤버접근 방법
     *  - 클래스이름.정적멤버이름
     *  - Static.static_field
     */

    System.out.println("1.Static.static_field:"+Static.static_field);
    Static.static_field=45465;
    System.out.println("2.Static.static_field:"+Static.static_field);
    Static.static_method();

    System.out.println("--------instance member access--------");

    Static static1 = new Static(); // 객체 선언 필수
    System.out.println(static1);
    static1.instance_field=555;
    System.out.println(static1.instance_field);
    static1.instance_method();

    Static static2 = new Static();
    System.out.println(static2);
    static2.instance_field=555;
    System.out.println(static1.instance_field);
    static2.instance_method();


    System.out.println("--------객체의 주소 사용해서 static멤버접근-------");
    /*
     * The static field Static.static_field should be accessed in a static way
     */
    static1.static_field=1111;
    static1.static_method();
    System.out.println("static1.static_field : "+static1.static_field);
    System.out.println("static1.static_field : "+static2.static_field);
    System.out.println("static.static_field : "+Static.static_field);
}

}

  • 변수
    1. 지역변수(Local)
    • 메쏘드의 블록 안에 선언된 변수(매개변수)
    1. 멤버필드(변수)
    • 클래스의 블록안에 선언된 변수
    • 객체 필드. 메소드(변수) : 인스턴스 생성 되어야 사용할 수 있는 필드
    • 정적 필드, 메소드(변수) : 인스턴스 생성과 관계없이 사용 가능한 필드 = static

static 을 사용하여 객체 생성 없이도 출력 할 수 있는 경우 연습!!

Comments