/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package lang; /** * * @author (C)Y.D.Zakovryashin, 14.11.2022 */ public class DemoObject implements Cloneable { private int id = 12344; public static void main(String[] args) { DemoObject obj = new DemoObject(); System.out.println("obj: " + obj.id); try { DemoObject clone = (DemoObject) obj.clone(); System.out.println("clone: " + clone.id ); DemoObject clone2 = (DemoObject) clone.clone(); System.out.println("clone2: " + clone2.id ); } catch (CloneNotSupportedException e) { System.out.println("Error: " + e.getMessage()); } } @Override public Object clone () throws CloneNotSupportedException { DemoObject t = new DemoObject(); // id = 12344 // DemoObject t = (DemoObject) super.clone(); t.id = id + 1; // id = 12345 if ( t.id > 1000000) { throw new CloneNotSupportedException ("Id overflow"); } return t; } }