class Score{
String name;
int kor,
int eng;
int math;
int sum;
float aver;
}
Score[] arr = new Score[3];
arr | instance(new로 선언된 곳, 이 공간또한 레퍼런스들이다.) |
---|---|
주소(200) | 200-> null(arr[0]) / null(arr[1]) / null(arr[2]) |
arr[0] = new Score();
arr[1] = new Score();
arr[2] = new Score();
new Score()는 Score 클래스에 선언된 변수를 Heap 메모리 영역에 순서대로 만든다.
arr[0] --> | name | kor | eng | math | sum | aver |
---|
arr[1] --> | name | kor | eng | math | sum | aver |
---|
arr[2] --> | name | kor | eng | math | sum | aver |
---|
class Outer {
변수;
메소드;
public class Inner {
}
}
Outer 객체1 = new Outer();
Outer.Inner 객체2 = 객체1.new Inner();
public class Outer {
int a = 10;
private int b = 20;
static int c = 30;
// Inner class
class Inner {
public void print() {
System.out.println(a + " " + b + " " + c);
}
}
public static void main(String[] args) {
Outer outer = new Outer();
Outer.Inner inner = outer.new Inner();
inner.print();
}
}
class Outer {
변수;
메소드;
public static class Inner {
}
}
Outer.Inner 객체 = new Outer.Inner();
import 패키지명.*;
Score s1 = new Score();
public class ExamTest1 {
public static void main(String[] args) {
class Store {
String name;
int kor;
int eng;
int math;
int sum;
float aver;
}
Store s1 = new Store();
s1.name = "홍길동";
s1.kor = 100;
s1.eng = 90;
s1.math = 85;
s1.sum = s1.kor + s1.eng + s1.math;
s1.aver = (float) s1.sum / 3;
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s1.name, s1.kor, s1.eng, s1.math, s1.sum, s1.aver);
Store s2 = new Store();
s2.name = "임꺽정";
s2.kor = 90;
s2.eng = 80;
s2.math = 75;
s2.sum = s2.kor + s2.eng + s2.math;
s2.aver = (float) s2.sum / 3;
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s2.name, s2.kor, s2.eng, s2.math, s2.sum, s2.aver);
Store s3 = new Store();
s3.name = "유관순";
s3.kor = 80;
s3.eng = 70;
s3.math = 65;
s3.sum = s3.kor + s3.eng + s3.math;
s3.aver = (float) s3.sum / 3;
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s3.name, s3.kor, s3.eng, s3.math, s3.sum, s3.aver);
}
}
public class ExamTest2 {
static class Score {
String name;
int kor;
int eng;
int math;
int sum;
float aver;
static void calc(Score s) {
s.sum = s.kor + s.eng + s.math;
s.aver = (float) s.sum / 3;
}
}
public static void main(String[] args) {
Score s1 = new Score();
s1.name = "홍길동";
s1.kor = 100;
s1.eng = 90;
s1.math = 85;
Score.calc(s1);
printScore(s1);
Score s2 = new Score();
s2.name = "임꺽정";
s2.kor = 90;
s2.eng = 80;
s2.math = 75;
Score.calc(s2);
printScore(s2);
Score s3 = new Score();
s3.name = "유관순";
s3.kor = 80;
s3.eng = 70;
s3.math = 65;
Score.calc(s3);
printScore(s3);
}
static void printScore(Score s) {
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s.name, s.kor, s.eng, s.math, s.sum, s.aver);
}
}
public class ExamTest3 {
static class Score {
String name;
int kor;
int eng;
int math;
int sum;
float aver;
}
public static void main(String[] args) {
Score s1 = new Score();
s1.name = "홍길동";
s1.kor = 100;
s1.eng = 90;
s1.math = 85;
printScore(s1);
Score s2 = new Score();
s2.name = "임꺽정";
s2.kor = 90;
s2.eng = 80;
s2.math = 75;
printScore(s2);
Score s3 = new Score();
s3.name = "유관순";
s3.kor = 80;
s3.eng = 70;
s3.math = 65;
printScore(s3);
}
static void printScore(Score s) {
s.sum = s.kor + s.eng + s.math;
s.aver = (float) s.sum / 3;
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s.name, s.kor, s.eng, s.math, s.sum, s.aver);
}
public class ExamTest3 {
static class Score {
String name;
int kor;
int eng;
int math;
int sum;
float aver;
}
public static void main(String[] args) {
Score s1 = new Score();
s1.name = "홍길동";
s1.kor = 100;
s1.eng = 90;
s1.math = 85;
printScore(s1);
Score s2 = new Score();
s2.name = "임꺽정";
s2.kor = 90;
s2.eng = 80;
s2.math = 75;
printScore(s2);
Score s3 = new Score();
s3.name = "유관순";
s3.kor = 80;
s3.eng = 70;
s3.math = 65;
printScore(s3);
}
static void printScore(Score s) {
s.sum = s.kor + s.eng + s.math;
s.aver = (float) s.sum / 3;
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s.name, s.kor, s.eng, s.math, s.sum, s.aver);
}
public class ExamTest4 {
static class Score {
String name;
int kor;
int eng;
int math;
int sum;
float aver;
}
public static void main(String[] args) {
Score s1 = new Score();
s1.name = "홍길동";
s1.kor = 100;
s1.eng = 90;
s1.math = 85;
printScore(s1);
Score s2 = new Score();
s2.name = "임꺽정";
s2.kor = 90;
s2.eng = 80;
s2.math = 75;
printScore(s2);
Score s3 = new Score();
s3.name = "유관순";
s3.kor = 80;
s3.eng = 70;
s3.math = 65;
printScore(s3);
}
static void printScore(Score s) {
s.sum = s.kor + s.eng + s.math;
s.aver = (float) s.sum / 3;
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s.name, s.kor, s.eng, s.math, s.sum, s.aver);
}
}
public class ExamTest5 {
static class Score {
String name;
int kor;
int eng;
int math;
int sum;
float aver;
void calc() {
this.sum = this.kor + this.eng + this.math;
this.aver = (float) this.sum / 3;
}
}
public static void main(String[] args) {
Score s1 = new Score();
s1.name = "홍길동";
s1.kor = 100;
s1.eng = 90;
s1.math = 85;
s1.calc();
printScore(s1);
Score s2 = new Score();
s2.name = "임꺽정";
s2.kor = 90;
s2.eng = 80;
s2.math = 75;
s2.calc();
printScore(s2);
Score s3 = new Score();
s3.name = "유관순";
s3.kor = 80;
s3.eng = 70;
s3.math = 65;
s3.calc();
printScore(s3);
}
static void printScore(Score s) {
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s.name, s.kor, s.eng, s.math, s.sum, s.aver);
}
}
class Score{
String name;
int kor;
int eng;
int math;
int sum;
float aver;
void calc() {
this.sum = this.kor + this.eng + this.math;
this.aver = (float) this.sum / 3;
}
}
public class ExamTest6 {
public static void main(String[] args) {
Score s1 = new Score();
s1.name = "홍길동";
s1.kor = 100;
s1.eng = 90;
s1.math = 85;
s1.calc();
printScore(s1);
Score s2 = new Score();
s2.name = "임꺽정";
s2.kor = 90;
s2.eng = 80;
s2.math = 75;
s2.calc();
printScore(s2);
Score s3 = new Score();
s3.name = "유관순";
s3.kor = 80;
s3.eng = 70;
s3.math = 65;
s3.calc();
printScore(s3);
}
static void printScore(Score s) {
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s.name, s.kor, s.eng, s.math, s.sum, s.aver);
}
}
클래스가 많을 경우 유지보수하기 쉽도록 적절한 패키지로 분산 배치한다.
데이터 타입의 역할을 하는 클래스의 경우
보통 domain, vo(value object), dto(data transfer object) 라는 이름을 가진
패키지에 분류한다.
패키지가 다르면 modifier 옵션에 따라 접근 범위가 달라진다.
멤버의 접근 범위 설정
public: 모두 공개
protected: 서브 클래스와 같은 패키지의 멤버는 접근 가능
(default): 같은 패키지의 멤버는 접근 가능
private: 접근 불가! 그 멤버가 속한 클래스의 내부에서만 접근 가능
새로 만든 패키지, domain에 Score 클래스 생성
(ExamTest7과 다른 패키지에 생성되었기에 접근을 위해 모든 변수가 public으로 선언되어야 한다.)
public class Score{
public String name;
public int kor;
public int eng;
public int math;
public int sum;
public float aver;
public void calc() {
this.sum = this.kor + this.eng + this.math;
this.aver = (float) this.sum / 3;
}
}
import com.eomcs.oop.ex02.test2.domain.Score;
public class ExamTest7 {
public static void main(String[] args) {
Score s1 = new Score();
s1.name = "홍길동";
s1.kor = 100;
s1.eng = 90;
s1.math = 85;
s1.calc();
printScore(s1);
Score s2 = new Score();
s2.name = "임꺽정";
s2.kor = 90;
s2.eng = 80;
s2.math = 75;
s2.calc();
printScore(s2);
Score s3 = new Score();
s3.name = "유관순";
s3.kor = 80;
s3.eng = 70;
s3.math = 65;
s3.calc();
printScore(s3);
}
static void printScore(Score s) {
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s.name, s.kor, s.eng, s.math, s.sum, s.aver);
}
}
public class Score2 {
public String name;
public int kor;
public int eng;
public int math;
public int sum;
public float aver;
public Score2(String name, int kor, int eng, int math) {
this.name= name;
this.kor=kor;
this.eng=eng;
this.math=math;
this.calc();
}
public void calc() {
this.sum = this.kor + this.eng + this.math;
this.aver = (float) this.sum / 3;
}
}
import com.eomcs.oop.ex02.test2.domain.Score2;
public class ExamTest8 {
public static void main(String[] args) {
Score2 s1 = new Score2("홍길동", 100, 90, 85);
printScore(s1);
Score2 s2 = new Score2("임꺽정", 90, 80, 75);
printScore(s2);
Score2 s3 = new Score2("홍길동", 80, 70, 65);
printScore(s3);
}
static void printScore(Score2 s) {
System.out.printf("%s: %d, %d, %d, %d, %.1f\n", s.name, s.kor, s.eng, s.math, s.sum, s.aver);
}
}
public class ExamTest {
static class Store{
String name;
int kor;
int eng;
int math;
int sum;
float aver;
}
public static void main(String[] args) {
Store s1 = new Store();
s1.name = "홍길동";
Store.name = "홍길동"; // 오류, name이 non-static이기 때문.
}
}
먼저 클래스의 멤버변수중 모든 인스턴스에 공통된 값을 유지해야하는 것이 있는지 살펴보고 있으면, static을 붙여준다.
작성한 메서드 중에서 인스턴스 변수를 사용하지 않는 메서드에 대해서 static을 붙일 것을 고려한다. 일반적으로 인스턴스변수와 관련된 작업을 하는 메서드는 인스턴스메서드(static이 안붙은 메서드)이고 static변수(클래스변수)와 관련된 작업을 하는 메서드는 클래스메서드static이 붙은 메서드)라고 보면 된다.
instance는 독립적이기 때문에 자주 변하는 값, 독립적이어야 하는 값에 이용하면 좋다고 생각한다.
- instance 더 알아보기
면접관 : static키워드에 대해서 설명해보세요.
응시자 : static키워드를 쓰면, 객체를 생성하지 않고도 변수나 함수를 사용할 수 있습니다.
면접관 : 왜 static키워드를 쓰나요?
응시자 : 객체를 생성하지 않아도 되니까 편리하고 속도도 빠릅니다.
면접관 : 그렇다면 모든 변수와 함수에 static을 붙이는 것이 좋겠네요?
응시자 : 가능한한 static을 붙이는 것이 좋다고 생각합니다.
면접관 : 어떤 경우에 static을 붙일 수 있고, 어떤 경우에 static을 붙일 수 없습니까?
응시자 : ...
면접관 : 만일 당신이 새로운 클래스를 작성한다고 할 때, 어떤 경우에 static키워드를 사용해야한다고 생각합니까?
출처: https://vaert.tistory.com/101 [Vaert Street:티스토리]