File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Beginning Java and the fly likes some doubts in core java Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "some doubts in core java" Watch "some doubts in core java" New topic

some doubts in core java

Kaushik Baral

Joined: Aug 08, 2009
Posts: 23
i have some doubts in some core java topics. i am writing list of the questions. thanks a lot in advance.

1) can we clone a singleton instance? do we need to implement clonable interface for this? if so then why because clone method is available in Object class.
2) If we clone the object it will loose its singleton property. so to stop cloning what we can do?
3) if we serialize an object and De serialize it twice how many objects we will get?

fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 11172

If an object is meant to be a singleton, then you probably don't want to let it be cloned. Therefore, you should not implement the cloneable interface. And if you don't, calling the clone() method inherited from the Object class throws an exception, if i recall correctly.

So the answer to #2 is to not implement the cloneable interface.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Minh Nam
Ranch Hand

Joined: Sep 10, 2011
Posts: 57
Kaushik Baral wrote:
3) if we serialize an object and De serialize it twice how many objects we will get?

Why don't you write some code to find the answer?

Advanced java topics
Ove Lindström
Ranch Hand

Joined: Mar 10, 2008
Posts: 326

Good article on clone in Java.
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 38100
Or you might find the 3rd chapter of the old edition of Bloch's Effective Java™ here. That tells you about clone(). Remember it is the old edition.
It is sorta covered in the JavaRanch Style Guide.
subject: some doubts in core java
Similar Threads
Cloning in java
problem in clone() while making singleton
Cloning in java
clone methode