1๏ธโฃ์์ฑ์๋?
์์ฑ์๋ ๊ฐ์ฒด๋ฅผ ์ด๊ธฐํํ๋ ๋ฉ์๋์ด๋ค. ๊ฐ์ฒด๊ฐ ์์ฑ๋ ๋ ํธ์ถ๋๋ ๋ฉ์๋๋ก, new ์ฐ์ฐ์๊ฐ ์ธ์คํด์ค๋ฅผ ์์ฑํ๋ฉด, ์์ฑ์๋ ์ธ์คํด์ค ๋ณ์๋ค(ํ๋๊ฐ๋ค)์ ์ด๊ธฐํํ๋ค. (์์ฑ์๋ ๋ฆฌํด ํ์ ์ด ์๊ณ , ํด๋์ค ์ด๋ฆ๊ณผ ๊ฐ์์ผ ํ๋ค.)
class Test{
int a,b;
public Test() {
System.out.println("๊ธฐ๋ณธ ์์ฑ์");
}
public Test(int a){
System.out.println("๋งค๊ฐ๋ณ์ 1๊ฐ ์๋ ์์ฑ์");
}
public Test(int a, int b){
System.out.println("๋งค๊ฐ๋ณ์ 2๊ฐ ์๋ ์์ฑ์");
}
}
public class Constructor {
public static void main(String[] args) {
Test t1=new Test();
Test t2=new Test(1);
Test t3=new Test(1,2);
/*
๊ธฐ๋ณธ ์์ฑ์
๋งค๊ฐ๋ณ์ 1๊ฐ ์๋ ์์ฑ์
๋งค๊ฐ๋ณ์ 2๊ฐ ์๋ ์์ฑ์
*/
}
}
์ฐธ๊ณ ๐)
ํด๋์ค๋ฅผ ์ ์ํ ๋ ์์ฑ์๋ฅผ ์๋ตํ๋ฉด ์ปดํ์ผ๋ฌ๊ฐ ์๋์ผ๋ก ๊ธฐ๋ณธ ์์ฑ์(Default Constructor)๋ฅผ ์์ฑํด ์ค๋ค. ๊ธฐ๋ณธ ์์ฑ์๋ ํ๋๊ฐ์ด ์๋ ์์ฑ์๋ก ๋ค์๊ณผ ๊ฐ์ ํํ์ด๋ค.
class A{
A(){} //๊ธฐ๋ณธ ์์ฑ์
}
ํ์ง๋ง, ๊ธฐ๋ณธ ์์ฑ์๋ฅผ ์๋์ผ๋ก ๋ถ๋ฌ์ค๋ ๊ฒ์ ์์ฑ์๊ฐ ์๋ฌด๊ฒ๋ ์์ ๋ ๊ฐ๋ฅํ๋ฉฐ, ๋ง์ฝ ์ค๋ฒ๋ก๋ฉํ ๋ค๋ฅธ ์์ฑ์๊ฐ ์์ ๊ฒฝ์ฐ์๋ ๊ธฐ๋ณธ ์์ฑ์๊ฐ ํ์ํ ๊ฒฝ์ฐ, ์๋ตํ ์ง ๋ง๊ณ ์จ์ค์ผ ํ๋ค.
2๏ธโฃthis vs this()
this() ๋ฉ์๋๋ ์์ ์ด ์ํ ํด๋์ค์์ ๋ค๋ฅธ ์์ฑ์๋ฅผ ํธ์ถํ๋ ๊ฒฝ์ฐ์ ์ฌ์ฉํ๋ค. this() ๋ฉ์๋๋ฅผ ์ฌ์ฉํ๊ธฐ ์ํด์๋ ๋ค์ ๋๊ฐ์ง ์กฐ๊ฑด์ ๋ง์กฑํด์ผ ํ๋ค.
- this() ๋ฉ์๋๋ ๋ฐ๋์ ์์ฑ์ ๋ด๋ถ์์๋ง ์ฌ์ฉ ๊ฐ๋ฅ
- this() ๋ฉ์๋๋ ๋ฐ๋์ ์์ฑ์์ ์ฒซ์ค์ ์์นํด์ผ ํ๋ค.
class Test{
int a,b;
public Test() {
System.out.println("๊ธฐ๋ณธ ์์ฑ์");
}
public Test(int a){
this();
System.out.println("๋งค๊ฐ๋ณ์ 1๊ฐ ์๋ ์์ฑ์");
}
public Test(int a, int b){
this(a);
System.out.println("๋งค๊ฐ๋ณ์ 2๊ฐ ์๋ ์์ฑ์");
}
}
public class Constructor {
public static void main(String[] args) {
Test t1=new Test();
Test t2=new Test(1);
Test t3=new Test(1,2);
/*
๊ธฐ๋ณธ ์์ฑ์
๊ธฐ๋ณธ ์์ฑ์
๋งค๊ฐ๋ณ์ 1๊ฐ ์๋ ์์ฑ์
๊ธฐ๋ณธ ์์ฑ์
๋งค๊ฐ๋ณ์ 1๊ฐ ์๋ ์์ฑ์
๋งค๊ฐ๋ณ์ 2๊ฐ ์๋ ์์ฑ์
*/
}
}
this ํค์๋๋ ๋ค์๊ณผ ๊ฐ์ด ์์ฑ์์์ ์ธ์คํด์ค ๋ณ์์ ๋งค๊ฐ๋ณ์์ ์ด๋ฆ์ ๊ตฌ๋ถํ๊ธฐ ํ๋ค๋, ์ด๋ฅผ ๊ตฌ๋ถํด์ฃผ๊ธฐ ์ํ ์ฉ๋๋ก ์ฌ์ฉ๋๋ค.
class Student{
String name;
int age;
String mbti;
public Student(String name, int age, String mbti) {
this.name = name;
this.age = age;
this.mbti = mbti;
}
}
๋ชจ๋ ๋ฉ์๋์๋ ์์ ์ด ํฌํจ๋ ํด๋์ค์ ๊ฐ์ฒด๋ฅผ ๊ฐ๋ฆฌํค๋ this๋ผ๋ ์ฐธ์กฐ๋ณ์๊ฐ ์๋๋ฐ, ์ผ๋ฐ์ ์ธ ๊ฒฝ์ฐ ์ปดํ์ผ๋ฌ๊ฐ this.๋ฅผ ์ถ๊ฐํด์ฃผ๊ธฐ ๋๋ฌธ์ ์๋ตํ๋ค. ์ฆ, this๋ ์ธ์คํด์ค ์์ ์ ๊ฐ๋ฆฌํค๋ฉฐ this๋ฅผ ํตํด ์ธ์คํด์ค ์์ ์ ๋ณ์์ ์ ๊ทผํ ์ ์๋ค.
3๏ธโฃ๋ด๋ถ ํด๋์ค
ํด๋์ค ๋ด์ ์ ์ธ๋ ํด๋์ค๋ก, ์ธ๋ถ ํด๋์ค์ ๋ด๋ถ ํด๋์ค๊ฐ ๊ธด๋ฐํ ์ฐ๊ด๋์ด ์๋ ๊ฒฝ์ฐ ์ฌ์ฉํ๋ค. ๋ด๋ถ ํด๋์ค๋ฅผ ์ฌ์ฉํ๋ฉด ์ธ๋ถ ํด๋์ค์ ๋ฉค๋ฒ๋ค์ ์ฝ๊ฒ ์ ๊ทผ์ด ๊ฐ๋ฅํ๊ณ , ์ฝ๋์ ๋ณต์ก์ฑ์ ์ค์ผ ์ ์๋ค. ๋ํ, ์ธ๋ถ์ ์ผ๋ก ๋ถํ์ํ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ถ ์ ์์ด ์บก์ํ๋ฅผ ๋ฌ์ฑํ๋ ๋ฐ ์ ์ฉํ๋ค.
๋ด๋ถ ํด๋์ค์๋ ์ธ์คํด์ค ๋ด๋ถ ํด๋์ค, ์ ์ ๋ด๋ถ ํด๋์ค, ์ง์ญ ๋ด๋ถ ํด๋์ค๊ฐ ์๋ค.
class Outer { // ์ธ๋ถ ํด๋์ค
class Inner {
// ์ธ์คํด์ค ๋ด๋ถ ํด๋์ค
}
static class StaticInner {
// ์ ์ ๋ด๋ถ ํด๋์ค
}
void run() {
class LocalInner {
// ์ง์ญ ๋ด๋ถ ํด๋์ค
}
}
}
์ธ์คํด์ค ๋ด๋ถ ํด๋์ค
์ธ๋ถ ํด๋์ค์ ๋ฉค๋ฒ ๋ณ์ ์ ์ธ ์์น ์ ์ธํ๋ฉฐ, ๋ฉค๋ฒ ๋ด๋ถ ํด๋์ค ์ด๋ค. ์ธ๋ถ ์ธ์คํด์ค ๋ณ์์ ์ธ๋ถ ์ ์ญ๋ณ์์ ์ ๊ทผโญ
๋จ, ์ธ์คํด์ค ๋ด๋ถ ํด๋์ค๋ ๋ฐ๋์ ์ธ๋ถ ํด๋์ค๋ฅผ ์์ฑํ ์ดํ์ ์ฌ์ฉ ๊ฐ๋ฅํ๋ฉฐ, ๋ฐ๋ผ์ ์ ์ ๋ณ์์ ์ ์ ๋ฉ์๋๋ ์ธ์คํด์ค ๋ด๋ถ ํด๋์ค์ ์ ์ธํ ์ ์๋ค!!
class Outer {//์ธ๋ถ ํด๋์ค
private int num=1; //์ธ๋ถ ํด๋์ค ์ธ์คํด์ค ๋ณ์
private static int sNum=2; //์ธ๋ถ ํด๋์ค ์ ์ ๋ณ์
private InClass inClass;
public Outer(){
inClass=new InClass();
}
class InClass{//์ธ์คํด์ค ๋ด๋ถ ํด๋์ค
int inNum=10;//๋ด๋ถ ํด๋์ค์ ์ธ์คํด์ค ๋ณ์
//static int sInNum=5;->์ ์ธ ๋ถ๊ฐ
void test(){
//์ธ๋ถ ํด๋์ค์ ์ธ์คํด์ค ๋ณ์์ ์ ์ ๋ณ์์ ์ ๊ทผ ๊ฐ๋ฅ
System.out.println("Outer num= "+num);
System.out.println("Outer sNum= "+sNum);
}
}
public void testClass(){
inClass.test();
}
}
public class InnerClassExample{
public static void main(String[] args) {
Outer outer = new Outer();
outer.testClass();//๋ด๋ถ ํด๋์ค ๊ธฐ๋ฅ ํธ์ถ
}
}
์ ์ ๋ด๋ถ ํด๋์ค
์ ์ ๋ด๋ถ ํด๋์ค๋ ์ธ๋ถ ํด๋์ค์์ ์กด์ฌ์ ๋ฌด๊ดํ๊ฒ ํธ์ถํด ์ฌ์ฉํ ์ ์์ผ๋ฉฐ, ์ธ๋ถ ํด๋์ค์ ๋ฉค๋ฒ ๋ณ์ ์ ์ธ ์์น์ ์ ์ธํ๋ค. (๋ฉค๋ฒ ๋ด๋ถ ํด๋์ค) ์ธ๋ถ ์ ์ญ ๋ณ์์๋ง ์ ๊ทผ โญ.(์ธ๋ถ ์ธ์คํด์ค ๋ณ์์๋ ์ ๊ทผ โ)
class Outer {//์ธ๋ถ ํด๋์ค
private int num=1; //์ธ๋ถ ํด๋์ค ์ธ์คํด์ค ๋ณ์
private static intsNum=2; //์ธ๋ถ ํด๋์ค ์ ์ ๋ณ์
void getPrint(){
System.out.println("์ธ์คํด์ค ๋ฉ์๋");
}
static void getPrintStatic(){
System.out.println("์คํํฑ ๋ฉ์๋");
}
static class StaticInClass{//์ ์ ๋ด๋ถ ํด๋์ค
int inNum=10;//์ ์ ๋ด๋ถ ํด๋์ค์ ์ธ์คํด์ค ๋ณ์
static int sInNum=5;//์ ์ ๋ด๋ถ ํด๋์ค์ ์ ์ ๋ณ์
void test(){
// System.out.println("Outer num= "+num);-> non-static์ด์ฌ์ ์๋จ.
System.out.println("Outer sNum= "+sNum);//์ธ๋ถ ํด๋์ค์ ์ ์ ๋ณ์์ ์ ๊ทผ ๊ฐ๋ฅ
System.out.println("In Num= "+inNum);
System.out.println("In sInNum= "+sInNum);
// getPrint(); -> non-static์ด์ฌ์ ์๋จ
getPrintStatic();
}
}
}
public class InnerClassExample{
public static void main(String[] args) {
Outer.StaticInClass s = new Outer.StaticInClass();
s.test();
}
}
์ง์ญ ๋ด๋ถ ํด๋์ค
ํด๋์ค์ ๋ฉค๋ฒ๊ฐ ์๋ ๋ฉ์๋ ๋ด์์ ์ ์๋๋ ํด๋์ค๋ก, ์ง์ญ๋ณ์์ ์ ์ฌํ๊ฒ ๋ฉ์๋ ๋ด๋ถ์์๋ง ์ฌ์ฉ ๊ฐ๋ฅํ๋ค.
class Outer {//์ธ๋ถ ํด๋์ค
private int num1=1; //์ธ๋ถ ํด๋์ค ์ธ์คํด์ค ๋ณ์
private static intsNum=2; //์ธ๋ถ ํด๋์ค ์ ์ ๋ณ์
void test(){
int num2=5;
class LocalInClass{//์ง์ญ ๋ด๋ถ ํด๋์ค
private int inNum=1;
// private static int sInNum=2; -> ์ ์ธ ๋ถ๊ฐ
void getPrint(){
System.out.println("inNum= "+inNum);
System.out.println("outNum= "+num1);
System.out.println("methodNum= "+num2);
System.out.println("outSnum= "+sNum);
}
}
LocalInClass localInClass=new LocalInClass();
localInClass.getPrint();
}
}
public class InnerClassExample{
public static void main(String[] args) {
Outer outer=new Outer();
outer.test();
}
}
์ฐธ๊ณ ์๋ฃ)
์์ฑ์(Constructor)
https://jenkov.com/tutorials/java/constructors.html
https://www.programiz.com/java-programming/constructors
Java Constructors (With Examples)
Java Constructors In this tutorial, we will learn about Java constructors, their types, and how to use them with the help of examples. What is a Constructor? A constructor in Java is similar to a method that is invoked when an object of the class is create
www.programiz.com
๋ด๋ถ ํด๋์ค(Nested Class)
https://www.javatpoint.com/java-inner-class
Java Inner Class - javatpoint
Java inner class or nested class with member inner class, anonymous inner class, local inner class and java static nested class.
www.javatpoint.com
https://nothing-is-on-my-way.tistory.com/30
๐งก์ค์ฒฉ ํด๋์ค,์ธํฐํ์ด์ค & ์ต๋ช ๊ฐ์ฒด
1๏ธโฃ์ค์ฒฉ ํด๋์ค๋? ์ค์ฒฉ ํด๋์ค๋ ํด๋์ค ๋ด๋ถ์ ์ ์ธ๋ ํด๋์ค๋ฅผ ๋งํ๋ค. ๋ณดํต, ํด๋์ค๊ฐ ์ฌ๋ฌ ํด๋์ค์ ๊ด๊ณ๋ฅผ ๋งบ์ ๊ฒฝ์ฐ์๋ ๋ ๋ฆฝ์ ์ผ๋ก ํด๋์ค๋ฅผ ์ ์ธํด์ฃผ๋ ๊ฒ์ด ์ข์ผ๋, ํน์ ํด๋์ค์
nothing-is-on-my-way.tistory.com
์ฝ๋
'Java๐' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
Spring MVC - JPA ๊ธฐ๋ฐ ๋ฐ์ดํฐ ์ก์ธ์ค ๊ณ์ธต(1) (0) | 2023.05.06 |
---|---|
๐JVM์ GC(Garbage Collector) (0) | 2023.03.13 |
๐OOP์ ํด๋์ค, ํ๋, ๋ฉ์๋, static๊น์ง (0) | 2023.02.23 |
๋๊ธฐ์ & ๋น๋๊ธฐ์ (0) | 2023.02.16 |
int[] -> List๋ก ๋ณ๊ฒฝํ๊ธฐ, ArrayList->int[]๋ก ๋ณ๊ฒฝํ๊ธฐ (0) | 2023.01.27 |