8

I was looking into the String.java source code when I found that one of the constructor has "String" object as parameter. This seems simple but I am not able to digest it. For example:

public class Example {

    private String value;

    public Example() {
        // TODO Auto-generated constructor stub
    }

    public Example(Example e){
        value = e.getValue();
    }

    String getValue() {
        return value;
    }
}

While compiling class Example for the first time, the compiler would encounter the second constructor with 'Example' class object as parameter. At this point, how will it find it as it is still compiling this class?

aioobe
  • 413,195
  • 112
  • 811
  • 826
Paramvir Singh
  • 2,354
  • 1
  • 20
  • 20
  • The same as when Class1 refers to Class2 and Class2 refers to Class1. – Michael Laffargue Jul 13 '12 at 07:29
  • @MichaelLaffargue yes, I was concerned about this case also. Like, String class uses java.util.Arrays and Arrays uses String class. But this puzzle is solved by going through aiobe's answer. – Paramvir Singh Jul 13 '12 at 08:23

2 Answers2

14

When the class is compiled, all it needs access to is the declaration of the class, not the full implementation.


Put differently, when compiling the constructor

public Example(Example e) {
    value = e.getValue();
}

all it needs to know is that there exists a class named Example and that it has a method getValue. This information can be gathered in a separate pass over the source files, prior to actually trying to compile the code.

(Btw, a constructor doesn't work much differently from a method. At first glance it may seem like a constructor needs to be compiled before any method is compiled, but that reasoning mixes up compile-time issues with run-time issues.)

aioobe
  • 413,195
  • 112
  • 811
  • 826
  • When a class is compiled, all it needs to know is the name of the class - not it's compiled version. – Brad Jul 13 '12 at 07:30
-1

Whenever there's any Copy Constructor in a class, there's always one more constructor. Not having it won't give any error, but on runtime it won't work. Since first the object needs to be constructed using non-copy constructor and then it's reference is passed to the constructor of some other constructor.

It's like telling the other object that 'hey man, I got some properties already which is working fine, you can also use the same property.'

Remember that there's always a non-copy constructed object, so that other object can just copy it's properties.

0xC0DED00D
  • 19,522
  • 20
  • 117
  • 184
  • Another example could be like there has to be someone to complete the homework first in the class, so that other students in the class can copy it. – 0xC0DED00D Jul 13 '12 at 07:45
  • -1. This answer is plain wrong. I could easily imagine having a `MyLinkedList(MyLinkedList tail)` constructor accepting `null` as argument (or passing an object of a subclass of `MyLinkedList`). And even if this wasn't allowed, nothing prevents you from *compiling* a class with only a copy-constructor. Besides, this is far from what's being asked here. – aioobe Jul 13 '12 at 09:22
  • @aioobe Check my answer, I've written that not having a non-copy constructor won't give you any error at compile time. Having a condition in the constructor for null will make the constructor a non-copy one, where we don't copy the properties. Besides I am just explaining the asker how this works in laymen terms. – 0xC0DED00D Jul 13 '12 at 10:08
  • It's still plain wrong considering you could pass a subclass to the copy constructor, right? – aioobe Jul 13 '12 at 10:11
  • What I meant to say is there's always one Object present to pass in the copy constructor, so there's no possibility of getting into a loop there, which is the actual question, "how this is possible" (If I interpreted the question correctly) – 0xC0DED00D Jul 13 '12 at 10:17
  • He's asking a compile-time question, you have answered with a run-time answer. – aioobe Jul 13 '12 at 10:33
  • A program has to run after the compilation. If you don't know what'll happen at runtime how will you understand if the things will work on compile time or not. Referring to your answer, you said that the constructor needs to know the name of the class only at compile time, which is true. But why?? why is it so?? because you won't be getting any runtime error, as there's always an object available to pass. I answered the 'why' here in plain laymen term. – 0xC0DED00D Jul 14 '12 at 09:57