Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
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 |
Tags
- 파라미터
- static
- 멤버변수
- expand
- 상속
- Override
- 메서드
- 배열
- if문
- 클래스
- Interface
- Singleton
- length
- After effects
- while문
- 생성자
- private
- 조건문
- 형변환
- 반복문
- Java
- for문
- illustrator
- Overload
- super
- symbol
- Photoshop
- 변수
- switch문
- do while문
Archives
- Today
- Total
Ms'Note
static-1 본문
// 하나의 게시물을 표현하기 위한 JavaBeans
public class Article {
/* static은 모든 객체가 공유하는 값이다. static값은 클래스 이름을 통해서 접근해야 하며,
객체의 생성 여부에 상관 없이 사용이 가능하다. */
// 전체 게시물의 수를 표현하기 위한 데이터
private static int count = 0;
// 모든 게시물은 하나의 카테고리 안에 존재한다고 가정한다.
// 게시물의 분류를 구별하기 위한 데이터
private static String category;
// 글 번호
private int num;
// 글 제목
private String title;
// 작성일시
private String regDate;
public Article(int num, String title, String regDate) {
super();
this.num = num;
this.title = title;
this.regDate = regDate;
// 이 클래스에 대한 객체 생성 -> 게시물 신규 등록
// 게시물이 새로 등록될 때 마다, 전체 글 수를 의미하는
// count 변수가 1씩 증가한다.
// 전체 게시물 수는 모든 객체가 공유하는 값이므로
// static으로 생성되어야 한다.
count++;
}
public static int getCount() {
return count;
}
public static void setCount(int count) {
Article.count = count;
}
public static void getCategory() {
return category;
}
public static void setCategory(String category) {
Article.category = category;
}
public int getNum() {
return num;
}
public void setNum(int num) {
this.num = num;
}
public String getTitle() {
return title;
}
public String getRegDate() {
return regDate;
}
public void setRegDate(String regDate) {
this.regDate = regDate;
}
@Override
public String toString() {
return "글 분류=" + category + ",전체 글 수=" + count +
"Article [num=" + num + ", title=" + title + ", regDate=" + regDate + "]";
}
}
결과 확인을 통한 static 변수와 static 메서드의 관계 살펴보기
- 일반 메서드 : static 변수와 일반 변수 모두 사용 가능하며, 일반 메서드와 static 메서드도 호출할 수 있다.
- Static 메서드 : 반드시 static 변수만 사용해야 하며, static 메서드만 호출할 수 있다.
Main01
public class Main01 {
public static void main(String[] args) {
Article.setCategory("자유게시판");
Article a1 = new Article(1, "첫 번째 글 제목", "2014-01-01");
Article a2 = new Article(2, "두 번째 글 제목", "2014-01-01");
Article a3 = new Article(3, "세 번째 글 제목", "2014-01-01");
// 출력결과에서 모든 객체가 동일한 count값을 갖는다.
System.out.println(a1.toString());
System.out.println(a2.toString());
System.out.println(a3.toString());
System.out.println("-------------------");
// static 변수의 값을 변경하면, 모든 객체가 영향을 받는다.
Article.setCategory("공지사항");
System.out.println(a1.toString());
System.out.println(a2.toString());
System.out.println(a3.toString());
}
}
출력 결과
'IT > └▶Example Coding.JAVA' 카테고리의 다른 글
OtherClassType-1 (0) | 2020.08.27 |
---|---|
static-2 (0) | 2020.08.27 |
Interface (0) | 2020.08.25 |
Abstract (0) | 2020.08.24 |
Boxing-2 (0) | 2020.08.23 |
Comments