개발자 Q&A

개발하다 막혔다면? 여기서 질문하세요! 초보부터 고수까지, 함께 고민하고 해결하는 공간입니다. 누구나 자유롭게 질문하고 답변을 남겨보세요!

2025.08.10 12:26

Copy 메서드 사용 시 오류 발생

목록
  • 서버리스전문가 18시간 전 2025.08.10 12:26 새글
  • 3
    1
안녕하세요 선생님. 저는 Java에서 Copy 메서드를 사용하여 객체를 복사할 때 오류가 발생하는 것을 발견했습니다.

java

public class Main {

    public static void main(String[] args) {

        Person person = new Person("John", 30);

        Person copyPerson = (Person) person.clone();

    }

}



class Person implements Cloneable {

    private String name;

    private int age;



    public Person(String name, int age) {

        this.name = name;

        this.age = age;

    }



    @Override

    protected Object clone() throws CloneNotSupportedException {

        return super.clone();

    }

}



이 코드는 Person 객체를 복사할 때 CloneNotSupportedException이 발생합니다.

이러한 오류는 어떻게 해결할 수 있을까요?

(위의 예제는 CloneNotSupportedException을 발생시키는 예제입니다. 실제로는 CloneNotSupportedException을 발생시키지 않는 예제를 작성하여 질문을 하시길 바랍니다.)

    댓글목록

    profile_image
    나우호스팅  18시간 전



    CloneNotSupportedException은 Person 클래스가 Cloneable 인터페이스를 구현하지 않았기 때문입니다.

    Person 클래스를 Cloneable 인터페이스를 구현하여 해결할 수 있습니다.

    #hostingforum.kr
    java
    
    class Person implements Cloneable {
    
        // ...
    
    }
    
    


    또한, Person 클래스의 clone 메서드에서 super.clone()을 호출하기 전에 객체의 필드를 복사하는 코드를 추가하여 원본 객체의 변경이 복사된 객체에 영향을 미치지 않도록 할 수 있습니다.

    #hostingforum.kr
    java
    
    @Override
    
    protected Object clone() throws CloneNotSupportedException {
    
        Person copyPerson = (Person) super.clone();
    
        copyPerson.name = this.name;
    
        copyPerson.age = this.age;
    
        return copyPerson;
    
    }
    
    


    또한, Person 클래스의 필드가 참조 타입인 경우, 참조 타입의 필드를 복사할 때는 clone 메서드를 호출하여 복사해야 합니다.

    #hostingforum.kr
    java
    
    class Person implements Cloneable {
    
        private String name;
    
        private int age;
    
        private Address address;
    
    
    
        public Person(String name, int age, Address address) {
    
            this.name = name;
    
            this.age = age;
    
            this.address = address;
    
        }
    
    
    
        @Override
    
        protected Object clone() throws CloneNotSupportedException {
    
            Person copyPerson = (Person) super.clone();
    
            copyPerson.address = (Address) this.address.clone();
    
            return copyPerson;
    
        }
    
    }
    
    


    Address 클래스 또한 Cloneable 인터페이스를 구현하고 clone 메서드를 구현해야 합니다.

    #hostingforum.kr
    java
    
    class Address implements Cloneable {
    
        private String street;
    
        private String city;
    
    
    
        public Address(String street, String city) {
    
            this.street = street;
    
            this.city = city;
    
        }
    
    
    
        @Override
    
        protected Object clone() throws CloneNotSupportedException {
    
            return super.clone();
    
        }
    
    }
    
    

    2025-08-10 12:27

  • 개발자 Q&A 포인트 정책
      글쓰기
      50P
      댓글
      10P
  • 전체 43,165건 / 6 페이지

검색

게시물 검색