9

From this Artima article on clone vs copy constructor:

Object's clone method is very tricky. It's based on field copies, and it's "extra-linguistic." It creates an object without calling a constructor. There are no guarantees that it preserves the invariants established by the constructors. There have been lots of bugs over the years, both in and outside Sun, stemming from the fact that if you just call super.clone repeatedly up the chain until you have cloned an object, you have a shallow copy of the object.

What does Joshua Bloch mean by extra-linguistic?

Geek
  • 26,489
  • 43
  • 149
  • 227
  • 1
    I'd guess that he means it falls outside of the rules that are normally established in the Java language for object instantiation - id est, calling the object's constructor. – jonvuri Mar 08 '13 at 17:10
  • @Geek +1, you ask lotta interesting questions from the book `Effective java by Joushua Bloch`, especially questions on generics .. :) – PermGenError Mar 08 '13 at 17:14
  • @PermGenError I am hoping the questions that I have asked would help others too. Stackoverflow is just awesome. – Geek Mar 08 '13 at 17:16
  • 3
    The word "extra-linguistic" is extra-linguistic. – irreputable Mar 08 '13 at 17:19
  • 1
    One of the main reasons to write Latin is to sound clever, although you are not really sure about what you're saying and secretly hope that your audience won't understand the issue and that they are afraid to admit that they don't. – jarnbjo Mar 08 '13 at 17:29
  • @jambjo you mean to say Josh doesn't know what he is saying? – Geek Mar 08 '13 at 17:34

3 Answers3

9

He means something like "outside of the scope of Java".

Specifically in Java the "correct" way to create a new object is by using that Object's constructor. Many class writers rely on this assumption and code logic into their constructors - things like input validation or anything else you want to guarantee at construction time - this is what he calls "invariants established by the constructors". But cloning bypasses this basic constraint and creates a memory copy without invoking the constructor - hence it is "extra linguistic".

Technically, so does serialization.

DontDivideByZero
  • 1,171
  • 15
  • 28
radai
  • 23,949
  • 10
  • 71
  • 115
  • So what's a valid use case for Object clone? – Pacerier Oct 31 '14 at 23:51
  • @Pacerier - within your own project, where you have complete control over the code cloning is 100% valid, although some would prefer copy constructors. it only becomes an issue when you no longer control all the code and might run into different assumptions made by other devs - like if youre writing a library. – radai Nov 01 '14 at 05:39
0

Probably the fact that it isn't implemented in Java but it has a native in the Object class.

Random42
  • 8,989
  • 6
  • 55
  • 86
0

The extra-linguistic object creation mechanisms (meaning other than calling or chaining constructors) are:

  1. cloning
  2. serialization
  3. reflection
  4. btye-code generation
Dov Wasserman
  • 2,632
  • 17
  • 14