Notice
Recent Posts
Recent Comments
Link
«   2025/05   »
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 31
Archives
Today
Total
관리 메뉴

공부 일지

필드 (field) 개념 정리 본문

국비지원/JAVA

필드 (field) 개념 정리

모로노이 2024. 6. 22. 12:42
#필드 (field) 란
    1. 클래스의 변수, 클래스의 속성을 저장하는데 사용된다.
    2. 클래스 내에 선언되고 클래스의 메서드에 접근할 수 있다.
    3. 필드는 클래스 변수와 인스턴스 변수,지역 변수로 이루어져있다.
    
    	##인스턴스 변수
            1. 클래스의 인스턴스(객체) 마다 별도로 저장되는 변수.
            2. 객체가 생성될 때  메모리에 올라가며 각각의 값을 가진다.
            3. 객체의 쓰임이 다 할 경우 인스턴스 변수도 메모리에서 내려간다.
            4. 인스턴스 변수(참조변수)는 객체를 통해 접근할 수 있다.
            public class Mouse {

                public String model;
                public String color;

                // 생성자
                public Mouse(String model,String color) {
                    this.model = model;
                    this.color = color;
                }

                // 메서드
                public void display() {
                    Syste.out.println("Model : " + model + ", Color : " + color);
                }
            }

            public class Main {
                public static void main(String [] args) {
                    Mouse mouse = new Mouse("logitech","black");

                    mouse.dusplay();	// Model : logitech, Color : black
                    }
               }
	##클래스 변수
    		1. 클래스에 속하는 변수로,클래스의 모든 인스턴스가 공유하는 변수다.
       	        2. static 키워드를 사용하여 선언한다.
        	3. 클래스가 호출 될 때 메모리에 올라가며, 프로그램을 종료 할 때 까지 존재한다.
        public class Mouse {
            // 클래스 변수(cv)
            	public static int numberOfMouse;
            
            // 인스턴스 변수(iv)
            	public String model;
            	public String color;
            
            // 기본 생성자
            public Mouse() {	}
            
            // 매개변수 생성자
            public Mouse(String model, String color) {
            	this.model = model;
            	this.color = color;
            	numberOfMouse++;
            }
            
            // 클래스 메서드
            public static int getnumberOfMouse() {
            	return numberOfMouse;
           	}
            
            // 인스턴스 메서드
            public void display() {
            	System.out.println("Model : " + model + ", Color : " + color);
            }
       	}
        
        class Main {
        	public static void main(String [] args) {
            	Mouse mouse = new Mouse("Logiteah","Black");
                
                mouse.display();	// Model : Logiteah, Color : Black
                
            // 클래스 변수에 접근
            System.out.println("Number Of Mouse : " + Mouse.getnumberOfMouse());
            // Number Of Mouse : 2
	##지역변수
    		1. 지역 변수는 멤버변수(클래스 변수,인스턴스 변수)와는 다르다
        	2. 메서드 내에 선언 되고, 메서드 내에서만 사용 가능하다.
        	3. 스택 메모리에 저장되며 메서드가 종료되면 소멸한다.
                class Variable {
                    public static void main(String [] args) {
                        int localVariable); = 1;
                        System.out.println(localVariable);	// 1 출력
                    }
	##static
    		1. 이 키워드를 통해 선언된 변수는 클래스의 모든 인스턴스가 공유한다.
        	2. 클래스가 처음 생성 될 때 초기화되며, 모든 인스턴스가 같은 값을 가진다.
        	3. 모든 클래스에서 사용한다고 해서 정적 멤버라고 부른다 
        	4. 클래스 변수는 다른 클래스에서도 사용이 가능하며 접근 제한자에 따라 다르다.
                class StaticVariable {
                    static int num = 1;
                    static String str = "안녕하세요";
                }

                public class Variable {
                    public static void main(String[] args) {
                        System.out.println(StaticVariable.num); // 1 출력
                        System.out.println(StaticVariable.str);	// 안녕하세요 출력
                    }
                }

 

'국비지원 > JAVA' 카테고리의 다른 글

오버로딩 (Method Overloading) 개념 정리  (0) 2024.06.22
메서드 (Method) 개념 정리  (0) 2024.06.22
OOP 클래스와 객체 정리  (0) 2024.06.21
kh 정보교육원 15일차  (0) 2024.05.29
kh 정보 교육원 13,14일차  (0) 2024.05.28