제이제이
article thumbnail
5. Java 객체지향 문법(5) - 클래스 변수와 클래스 메소드

클래스 변수 🤔 클래스 변수란? static으로 선언된 변수로써, 선언된 클래스의 모든 인스턴스(객체)가 공유하는 변수를 의미합니다. 클래스 변수 선언 클래스 내에 선언된 변수 앞에 static 선언을 붙이면 인스턴스 변수가 아닌 클래스 변수가 됩니다. static 클래스변수명 다음의 예를 통해 자세히 이해 해봅시다. 💻 예제 코드 class InstCnt{ static int instNum = 0; //생성자 InstCnt() { instNum++; //static으로 선언된 변수의 값을 증가 System.out.println("인스턴스 생성: " + instNum); } } public class ClassVar { public static void main(String[]args){ //객체 생성 ..