
public으로 지정할 수 있다.🍏public class Car { ... }
class Tire { ... }
객체의 데이터가 저장되는 곳)초기화 ✨역할 담당)동작에 해당하는 실행 블록)public class ClassName{
//필드
int fieldName;
//생성자
className() {...};
//메소드
void methodName(); {...}
}

Korean(); 라는 생성자를 안 만들어도, 다른 실행클래스에서
이렇게 Korean 클래스의 객체를 만들 수 있다는 것이다.
✨✨✨✨✨하지만!!!✨✨✨✨✨
개발자가 생성자를 따로 만든다면, 
▲그니까 이렇게 생성자를 만든다면,
✨✨컴파일러는 자동으로 기본생성자 Korean();을 만들지 않는다!✨✨
그래서 실행클래스에서 기본생성자로 객체를 만들려고 하면,

▲ 이렇게 오류가 날 것이다!
그리고
String country;
라고 라이브러리 클래스에서 지정하지 않고
String country = "대한민국"
이라고 지정해놓고, 실행 클래스에서
korean.country = "영국";
으로 바꿔도 바꿀 수 있다! 그러니까, default로 만들어놓은 필드도 객체마다 변경이 가능하다는 것.
(마찬가지로,

이렇게 default값을 줬더라도 생성자를 통해서 다른 값으로 바꿔서 객체생성하도록 생성자 설정이 가능하다. 그러니까, default값은 변경 가능한 default라는 것!

public class Car {
Car() {...}
Car(String model) {...}
Car(String model, String color) {...}
Car(String model, String color, int maxSpeed) {...}
}
또는
Car(String model, String color, int maxSpeed) {...}
}
Car(String model, int maxSpeed, String color) {...}
}
Overloading 아닌 예
Car(String color, String color, int maxSpeed) {...}
Car(String color, String color, String color) {...}




Korean(String country, int age, int ssn) {
this.country = country;
this.age = age;
this.ssn = ssn;
}
>>이 블럭은 구조체(?)처럼 만들어 놔야하는 것. (하 뭔소린지 모르겠다면 이 말은 지우자^^)
Car(String model) {
this(model, null,0); >> 얘가 첫 줄에 와야함
System.out.println("어쩌구 저쩌구");
}
double divide(int x, int y) { ... }
byte v1 = 10;
byte v2 = 20;
double result = divide (10, 20);
처럼 int로 메소드 선언해놓고 byte타입 매개값을 넣어도 된다는 말.
(byte > int)는 자동 타입변환이 가능하므로.
방법1) 배열 생성해서 넘겨주는 방법
int sum(int[] values) { ... }
-----------------------------
int[] values = {1, 2, 3}; ❓아놔 왜 new 안 쓰지...❓
방식1) int result = sum(values);
방식2) int result = sum(new int[] {1, 2, 3, 4, 5});
❓ 왜 new를 안 쓰는지는 직접 사용해보면 알게 된다.

방법2) 배열 생성하지 않고 넘겨주는 방법 >> 궁극적으로는 배열이 생성되고 배열을 이용하게 됨.
int sum(int ... values) { ... }
-----------------------------
int result = sum(1,2,3);
int result = sum(1,2,3,4,5);
✅방법1과 방법 2 모두 배열이 생성된다!✅
public int sum1(int[] values) {
int sum = 0;
for (int i =0; i < values.length; i++) {
sum += values[i];
}
return sum;
}
public int sum2(int ... values) {
int sum = 0;
for (int i =0; i < values.length; i++) {
sum += values[i];
} >>받을 땐 배열로 안 받지만 내부 동작할 땐 배열로 동작함
return sum;
}
public class Calculator {
double avg(int x, int y) {
double sum = x + y;
retrun sum;
}
void execute() {
double result = avg(7,10);
System.out.println("실행결과: " + result);
}
}int plus(int x, int y) {
int result = x + y;
return result;
System.out.println(result) //컴파일 오류
}
void run() {
while(true) {
if (has > 0) {
System.out.println("달립니다");
} else {
System.out.println("멈춥니다");
return;
} } System.out.println("배고파요"); } >> "배고파요" 출력 안 됨
void run() {
while(true) {
if (has > 0) {
System.out.println("달립니다");
} else {
System.out.println("멈춥니다");
break;
} } System.out.println("배고파요"); } >> "배고파요" 출력 됨
public class Car {
int gas;
void setGas(int gas) {
this.gas = gas;
}
boolean isLeftGas() {
if (gas == 0) {
System.out.println("gas가 없습니다");
return false;
}
System.out.println("gas가 있습니다"); >> 실행문은 return문 앞에 위치.
return true;
}
void run() {
while (true) {
if (gas>0) {
System.out.println("달립니다. gas 잔량" + gas);
gas--;
} else {
System.out.println("멈춥니다. gas 잔량" + gas);
return;
}
}
}
}
public class CarExample {
public static void main(String[] args) {
Car mycar = new Car();
mycar.setGas(5);
boolean gasState = mycar.isLeftGas();
if(gasState) {
System.out.println("출발합니다");
mycar.run();
}
if(mycar.isLeftGas()) { >> if문 안에 메소드 호출하는 것도 사용 가능!
System.out.println("gas를 주입할 필요가 없습니다");
} else {
System.out.println("gas를 주입하세요");
}
}
}
public class Car {
int speed;
int getSpeed() {
return speed;
}
void keyTurnOn() {
System.out.println("차키를 돌립니다");
}
void run() {
for (int i=10; i <=50; i+=10) {
speed = i;
System.out.println("달립니다. 시속 : " + speed);
}
}
}
public class CarExamplle {
public static void main(String[] args) {
Car myCar = new Car();
myCar.keyTurnOn();
myCar.run();
int speed = myCar.getSpeed();
System.out.println("현재속도" + speed);
}
}
매개변수의 타입, 개수, 순서가 달라야한다.타입이 다른 메소드 오버로딩 case
int plus(int x, int y) {
return x+y;
}
double plus(doublex, double y) {
return x+y;
}
x =10, y =20일땐 첫번째 메소드가 선택됨
x = 10.5, y = 20.3일땐 두번째 메소드가 선택됨
x=10, y=20.3일땐 두번째 메소드가 선택됨
int duvide(int x, int y) { ...}
double divide(int boonia, int boonmo) {...}
>>오버로딩(X)
>>왜? 어떤 매소드를 호출해야하는지 자바가 알 수 없음
static 키워드를 붙이면 된다.public class 클래스 {
//정적필드
static 타입 필드[=초기값];
//정적 메소드
static 리턴타입 메소드(매개변수 선언, ...) {...}
}
클래스. 필드;
클래스. 메소드(매개값,...);
public Calculator {
String color;
void setColor(String color) {this.color = color;} >>static 불가❌
왜? 객체가 없으니 가르킬 this가 없어서
static int plus(int x, int y) {return x + y} >>static 가능
static int minus(int x, int y) {return x - y} >>static 가능
this.color는 인스턴스 멤버라서 객체마다 가지는 값들로, 객체를 일단 만들어야 생성되는 것임. (위에서 말했듯.) 그런데 static을 붙이면 객체 생성없이 사용할 수 있어야 하는데 그게 안되니까(두 논리가 충돌하잖음) 그래서 불가.
▶ 정적메소드와 정적블록을 선언할 때 주의할 점은 객체가 없어도 실행된다는 특징 때문에, 이들 내부에 인스턴스 필드나 인스턴스 메소드를 사용할 수 없다.
(강의) 21:40
✅ 그럼 어떻게 해야 값 초기화가 가능한가?
-> 초기화 블록 사용!

public class Television {
static String company = "삼성";
static String model = "LCD";
static String info;
static String info;
static {
info = company + "-" + "model; >>이거 하나
int sum = 0;
for (int i =0; i<10;i++)
{ sum += i; } >>또 하나
}
>>같은 static 블록 하나 안에 여러 static 값 설정들을 해서 묶어버리는군.
저 static { ...} 블록 안에는 instace 멤버 불가, instance 필드 불가!
오로지 static ~어쩌구~ 들만 필드처럼 지정할 수 있음
System.out.println(Television.info);
(내 생각: 용도가 뭐지..? 굳이 있는 이유를 모르겠네...)

//정적 필드
private static 클래스 singleton = new 클래스();
//생성자
private 클래스 () {}
//정적 메소드✔
static 클래스 getInstance() {
return singleton;}
✔ singleton 객체를 리턴해 줄 때는 static 메소드
✔ 외부에서 싱글톤 객체를 얻는 유일한 방법은 getInstace() 메소드를 호출하는 방법 뿐!!!!!!
Singleton obj1 = new Singleton(); ❌
Singleton obj2 = new Singleton(); ❌
Singleton obj1 = Singleton.getInstance();
Singleton obj2 = Singleton.getInstance();
if (obj1 == obj2) {
System.out.println("같은 객체입니다");
} else {
System.out.println("다른 객체입니다.");
} >> "같은 객체입니다."
클래스 생성 초반에 만들기 ex) 국적생성자 만들 때 만들기 ex) 주민번호(주민으로 등록할 때)final String nation = "Korea"; >>첫 번째 방법
final String ssn; >>두 번째 방법
Math.PI_로 연결하는 것이 관례static final 타입 상수;
static {
상수 = 초기값;
}


이렇게 하면 패키지가 생성되지 않고 클래스만 생성된 것을 폴더에서 확인할 수 있음.

이렇게 하면 됨.

pckage com.mycompany;
public class Car {
com.hankook.Tire tire = new com.hankook.Tire();
}
2) 보통 이 방법 사용
- import문으로 패키지를 지정하고 사용
package com.company;
import com.hankook.Tire;
[또는 import com.hankook.*;] >>그 패키지 안 모든 클래스 import
public class Car {
Tire tire = new Tire();
}
2)번 방법에서 *로 할 경우 미리 설정을 바꿔야 함. 방법은

1~2번까지 한 후,
public class Car {
Tire tire = new Tire();
}
작성한 후 컴파일 에러가 난 상태에서 Ctrl + shift + O하면
import com.hankook.*;
가 생김. 이렇게 설정 안 바꾼 상태에서 Ctrl + shift + O하면
import com.hankook.Tire;
이렇게 넣어짐.
*가 아니가 .Tire를 추가로 import해줌
근데 또 다른 패키지의 Tire클래스가 필요할 수 있으니.. 이럴 땐 위에 import문을 삭제하고
이렇게 하나하나 다 입력하세요. ㅎ
default와 public만 사용 가능source > generate getter and setter private 필드의 값을 리턴하는 역할읽기 전용getFieldName() 메소드를 말한다. 단, 타입이 boolean일 경우 isFieldName()유효성 검사한다.setFieldName(타입변수) 메소드를 말한다.