Solved

Clone

Posted on 2002-07-28
4
305 Views
Last Modified: 2010-03-31
here is my problem.............

i have a class object created in different class.
in another class i want the same object to be returned .
i tried to do this using clone concept.
i am not getting the result what i expected.
i have a piece of code.
please guide me where i am going wrong & how to fix the same.

class Base extends Object implements Cloneable
{
     public void setCount(int count)
     {
     }
     public int getCount()
     {
          return 0 ;
     }
     public Object clone()
     {
          try
          {
               return super.clone() ;
          }
          catch(CloneNotSupportedException e)
          {
               return this ;
          }
     }
};

class TestClone extends Base
{
     int Count = 0 ;
     public TestClone()
     {
     }
     public TestClone(int data)
     {
          Count = Count + 1 ;
          setCount(Count) ;
     }
     public void setCount(int val)
     {
          Count = val ;
     }
     public int getCount()
     {
          return Count ;
     }
}

class Demo2
{
     public Demo2()
     {
          baseObj1 = new TestClone() ;
          baseObj = (Base)baseObj1.clone() ;

          int i = baseObj.getCount() ;
          System.out.println("clone value = " + i );
     }
     private Base baseObj = null  ;
     private Base baseObj1 = null ;
};

class DemoClone
{
     public static void main(String[] args)
     {
          baseObj = new TestClone(5) ;
          dm = new Demo2() ;
     }
     static private Base baseObj ;
     static private Demo2 dm ;
};

in my demo2 class i expect the output to displayed as 1.
because count would have been incremented by 1.

if this is wrong please guide me how to get the required output.

thanks in advance.

0
Comment
Question by:umaiyer
  • 3
4 Comments
 
LVL 92

Expert Comment

by:objects
ID: 7184326
> because count would have been incremented by 1.

Why would you expect this, you do not call the ctor that increments count.
try this instead:

class Demo2
{
    public Demo2()
    {
         baseObj1 = new TestClone(0) ;
         baseObj = (Base)baseObj1.clone() ;

         int i = baseObj.getCount() ;
         System.out.println("clone value = " + i );
    }
    private Base baseObj = null  ;
    private Base baseObj1 = null ;
};
0
 

Author Comment

by:umaiyer
ID: 7184344
in my DemoClone class i am calling a ctor of TestClone & incrementing the Count to 1.
in demo2 class i do not want to increment count value.
Now i want to access this count value in my Demo2 class & i wants the value to get 1 .
how to acheive this.

0
 
LVL 92

Expert Comment

by:objects
ID: 7184429
The object you're printing the value of is a clone of baseObj1 which has not been increemented.
0
 
LVL 92

Accepted Solution

by:
objects earned 100 total points
ID: 7184435
class Demo2
{
    public Demo2()
    {
         baseObj1 = new TestClone() ;
         baseObj = (Base)baseObj1.clone() ;

         int i = DemoClone.baseObj.getCount() ;
         System.out.println("clone value = " + i );
    }
    private Base baseObj = null  ;
    private Base baseObj1 = null ;
};

class DemoClone
{
    public static void main(String[] args)
    {
         baseObj = new TestClone(5) ;
         dm = new Demo2() ;
    }
    static public Base baseObj ;
    static private Demo2 dm ;
};
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
firstChar challenge 13 107
best (free) software to access postgres db (java) 1 32
HashMap Vs TreeMap 12 55
expectj telnet failing 5 23
For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…

910 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

24 Experts available now in Live!

Get 1:1 Help Now