Object is on the top of Java classes.
String is a subclass of Object.
So, what was first – Object or String?
The prompt answer is – Object.
But the interesting thing is that Object already has a method toString(), and thus “knows” about String. So, when Object is constructed, the String has to already exist. On the other hand, String is a subclass of Object, and when String is constructed Object has to already exist. We fall into a never ending definition cycle (and some technical problem, too). Such approach at least violates the idea of a single root class.
The concern may look like rather theoretical, than practical.
But the thing is that I see a similar approach in other frameworks.
I think at least to some extent it was inspired by the way core Java classes were design.
What do you think – are circular dependencies between Java classes in general (and particular in case of Object/String) inevitable?
Shouldn’t they be avoided at any cost?
Or can they be accepted sometimes (with discretion and caution) as the result of a reasonable compromise? If, so – what are the criteria?
Java hasn’t a one-pass compiler but a multi-pass compiler.
This means that all classes that are compiled together are really at the declaration level. Even if they have circular dependencies these are solved in a first step so the fact that
Objectclass provides atoStringmethod does not conceptually mean anything in relation to having or less a root class.Since we’re talking about theoretical issues the relation between a class declaration and and which is the real root class is resolved easily:
Objectis the root class just becauseStringinherits from it.The
String toString()is nothing more that a signature that is useful to the compiler to grant type safety,Objectdoesn’t require aStringobject, it doesn’t even need to know what aStringis.