Solved

Clone

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

Maximize Your Threat Intelligence Reporting

Reporting is one of the most important and least talked about aspects of a world-class threat intelligence program. Here’s how to do it right.

Join & Write a Comment

Suggested Solutions

Title # Comments Views Activity
countHi2 challenge 7 44
mapBully challenge 6 89
where is session ID cache stored 1 39
HSSFWorkbook cannot be resolved error 10 16
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
Viewers will learn one way to get user input in Java. Introduce the Scanner object: Declare the variable that stores the user input: An example prompting the user for input: Methods you need to invoke in order to properly get  user input:
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …

706 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

18 Experts available now in Live!

Get 1:1 Help Now