Solved

Clone

Posted on 2002-07-28
4
326 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Salesforce Made Easy to Use

On-screen guidance at the moment of need enables you & your employees to focus on the core, you can now boost your adoption rates swiftly and simply with one easy tool.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Unhandled exception type Exception 18 65
Java string replace 11 80
pagenation logic how it is working in my code 1 82
American Express @Work site and Java 4 97
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Suggested Courses

710 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