inflearn logo
강의

강의

N
챌린지

챌린지

멘토링

멘토링

N
클립

클립

로드맵

로드맵

지식공유

자바 클래스 선언 오류

315

보니베어

작성한 질문수 1

0

public class EE {
     int a = 1;


        EE ee = new EE();

        ee.a = 2;

}

이렇게하면 오류가 나고

여기서 블록을 하나 더 감싸면 오류가 안나더라구요


public class EE {
     int a = 1;


       EE ee = new EE();

      {  ee.a = 2 };

}

무슨 이유에서일까요?

클래스 선언 블록에서는 무언가 동작을 하는 코드를 넣을수 없는건가요?

답변 1

0

negigulshan903

Well, here is the simple example how to declare class in Java.

public class Person {
    // Class members (fields, methods, constructors, etc.) go here
    
    // Example field (instance variable)
    private String name;
    
    // Example constructor
    public Person(String name) {
        this.name = name;
    }
    
    // Example method
    public void sayHello() {
        System.out.println("Hello, my name is " + name);
    }
}

Thanks

강의 수강 후 포트폴리오 준비 방향에 대해 조언 부탁드립니다.

2

52

1

프론트엔드 이력서 관련 질문

1

46

2

강의가 재생되지 않습니다.

1

33

1

매일 사용시 토큰사용량

1

57

2