Solved

Clone

Posted on 2002-07-28
4
319 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

Networking for the Cloud Era

Join Microsoft and Riverbed for a discussion and demonstration of enhancements to SteelConnect:
-One-click orchestration and cloud connectivity in Azure environments
-Tight integration of SD-WAN and WAN optimization capabilities
-Scalability and resiliency equal to a data center

Question has a verified solution.

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

Suggested Solutions

Are you developing a Java application and want to create Excel Spreadsheets? You have come to the right place, this article will describe how you can create Excel Spreadsheets from a Java Application. For the purposes of this article, I will be u…
Introduction Java can be integrated with native programs using an interface called JNI(Java Native Interface). Native programs are programs which can directly run on the processor. JNI is simply a naming and calling convention so that the JVM (Java…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…
This theoretical tutorial explains exceptions, reasons for exceptions, different categories of exception and exception hierarchy.

820 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