[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 59
  • Last Modified:

Need help on Clonning objects in Java

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
sam_2012
Asked:
sam_2012
1 Solution
 
CPColinCommented:
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
 
sam_2012Author Commented:
Yes , My question is , is i the right way to create an count of objects for an Class  using constructors and clone method .
0
 
krakatoaCommented:
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
 
gurpsbassiCommented:
can you also share the test class - EmployeeTest.java?
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now