1.
class Animal {
String type = "Canine";
int maxSpeed = 60;
Animal() { }
Animal(String type, int maxSpeed) {
this.type = type;
this.maxSpeed = maxSpeed;
}
}
class WildAnimal extends Animal {
String bounds;
WildAnimal(String bounds) {
//line n1
}
WildAnimal(String type, int maxSpeed, String bounds) {
//line n2
}
}
public class Test {
public static void main(String[] args) {
WildAnimal wolf = new WildAnimal("Long");
WildAnimal tiger = new WildAnimal("Feline", 80, "Short");
System.out.println(wolf.type + " " + wolf.maxSpeed + " " + wolf.bounds);
System.out.println(tiger.type + " " + tiger.maxSpeed + " " + tiger.bounds);
}
}
- line n1에는 A: super();
this.bounds = bounds; - line n2에는 E: super(type, maxSpeed);
this.bounds = bounds;
2.
public class Main {
public static void main(String[] args) {
String names[] = {"Thomas", "Peter", "Joseph"};
String pwd[] = new String[3];
int idx = 0;
try {
for (String n : names) {
pwd[idx] = n.substring(2, 6);
idx++;
}
} catch (Exception e) {
System.out.println("Invalid Name");
}
for (String p : pwd) {
System.out.println(p);
}
}
}
- "Thomas": 길이가 6글자이므로, substring(2, 6)은 정상적으로 "omas"를 반환합니다. pwd[0]에 저장
- "Peter": 길이가 5글자이므로, substring(2, 6)을 호출하면 인덱스 초과로 StringIndexOutOfBoundsException이 발생
"Invalid Name""omas""null""null" > 캐치문 먼저 출력후 그 다음에 pwd 출력, 들어간 게 없으면 null로 출력
3.
class Test {
// 1. public만 있는 경우 (인스턴스 메소드)
public int[] findMax(int[] numbers) {
// 사용하려면:
Test test = new Test(); // 객체 생성 필요!
test.findMax(numbers); // 객체를 통해 호출
}
// 2. public static인 경우 (클래스 메소드)
public static int[] findMax(int[] numbers) {
// 바로 사용 가능:
Test.findMax(numbers); // 객체 생성 없이 호출
// 또는 그냥
findMax(numbers); // 같은 클래스 내에서
}
public static void main(String[] args) {
// main은 static이므로
// static이 아닌 메소드는 직접 호출 불가!
int[] numbers = {12, 13, 42};
findMax(numbers); // static 메소드만 가능
}
}
- new Test(): 클래스의 새로운 인스턴스(객체) 생성
- new int[3]: 그냥 int 타입 값 3개를 저장할 수 있는 공간 할당
4.
- A. A public class must have a main method.
- ❌ : main 메소드는 프로그램의 시작점일 뿐
- 모든 클래스가 가질 필요는 없음
- B. A class can have only one private constructor.
- ❌ : 여러 개의 private 생성자 가능
- C. A method can have the same name as a field.
- ✅ : 자바에서는 메서드와 필드가 같은 이름을 가질 수 있음
- 메서드 호출 시에는 ()가 붙으므로, 메서드와 필드를 구분 가능
- D. A class can have overloaded static methods.
- ✅ : static 메서드는 오버로딩 가능
- E. The methods are mandatory components of a class.
- ❌ : 클래스에 메서드가 꼭 있어야 하는 것은 아님. 필드만 존재 가
- F. The fields need not be initialized before use.
- ✅ : 필드는 자동으로 기본값 초기화됨
5.
public class App {
int count; // 인스턴스 변수
public static void displayMsg() {
count++; // line n1 - 에러!
// static 메소드에서 인스턴스 변수 접근 불가
System.out.println("Welcome "+"Visit Count: "+count); // line n2 - 에러!
}
public static void main(String[] args) {
App.displayMsg(); // line n3
App.displayMsg(); // line n4
}
}
- static 메소드에서는 static 멤버만 직접 접근 가능
- count는 인스턴스 변수(non-static)
- 따라서 static 메소드인 displayMsg에서 count 접근 불가
>>>> 해결방법
1. count를 static으로 선언
2. static 메서드 안에 객체생성해서 객체.count로 불러오기
6.
static int count = 0; // static 변수 : 모든 객체가 공유하는 값
int i = 0; // 인스턴스 변수 : 각 객체마다 별도의 값
// 1. static 메소드에서:
public static void main() {
count++; // static 변수 직접 접근 가능
i++; // 인스턴스 변수 직접 접근 불가능
}
// 2. 인스턴스 메소드에서:
public void changeCount() {
count++; // static 변수 접근 가능!
i++; // 인스턴스 변수도 접근 가능!
}
static 변수 : 모든 객체가 공유하는 값
인스턴스 변수 : 각 객체마다 별도의 값
>>헷갈렸던 점
static 메서드에서는 static 변수만 접근 가능하지만
static 변수자체는 어디에든 접근 가능
7.
ArrayList<Integer> points = new ArrayList<>();
points.add(1); // [1]
points.add(2); // [1,2]
points.add(3); // [1,2,3]
points.add(4); // [1,2,3,4]
points.add(null);// [1,2,3,4,null]
points.remove(1);// [1,3,4,null] - 인덱스 1의 값(2)를 제거
points.remove(null);// [1,3,4] - null 값을 제거
System.out.println(points); // [1,3,4]
실제 1의 값을 제거하고 싶다면
points.remove(Integer.valueOf(1)); 이런식으로 해야함
8.
배열 새로 생성하면 기본 초기화 0으로 세팅
'Certification > OCAJP' 카테고리의 다른 글
OCAJP 준비 (Q126 - Q145) (1) | 2024.11.18 |
---|---|
OCAJP 준비 (Q106 - Q125) (8) | 2024.11.13 |
OCAJP 준비 (Q66 - Q90) (2) | 2024.11.07 |
OCAJP 준비 (Q45 - Q65) (0) | 2024.11.06 |
OCAJP 준비 (Q26 - Q44) (0) | 2024.11.05 |