Solved

Need help on Clonning objects in Java

Posted on 2016-08-01
4
41 Views
Last Modified: 2016-08-06
Hi Team,

Below is the code which I have wriiten to check the number of instances created for an Class called as Employee.
 
Somehow the code does not gives the expected output. Iam used a method called as checkinstances to check the number of instances created for the Employee class .

If it is equal to 3 , I throwing an exception. Also Iam checking the number of intances created using the overridden method called CheckInstances in both Clone and in constructor.

Any help in is really appreciated.
Employee.java
0
Comment
Question by:sam_2012
4 Comments
 
LVL 14

Expert Comment

by:CPColin
ID: 41737814
I don't get the error you're getting. Is that the exact code you're running?

Try adding some debug logging in every place where you're incrementing your counter. Have it log something like "In Employee()" or "In clone()" and see how many times each operation is happening.
0
 

Author Comment

by:sam_2012
ID: 41738155
Yes , My question is , is i the right way to create an count of objects for an Class  using constructors and clone method .
0
 
LVL 16

Accepted Solution

by:
krakatoa earned 500 total points
ID: 41738227
Your code would count the instances correctly as far as I can see. You increment the count twice, and two objects are created, which seems ok.
1
 
LVL 15

Expert Comment

by:gurpsbassi
ID: 41738956
can you also share the test class - EmployeeTest.java?
0

Featured Post

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

An old method to applying the Singleton pattern in your Java code is to check if a static instance, defined in the same class that needs to be instantiated once and only once, is null and then create a new instance; otherwise, the pre-existing insta…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
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:
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

743 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

11 Experts available now in Live!

Get 1:1 Help Now