Constructor match ordering and sub-classes

R

raphfrk

If you have 2 constructors for an object will it always match the most
specific.

public class ConstTest {

ConstTest(int x) {
System.out.println("Int: " + x );
}

ConstTest(Object o) {
System.out.println("Object: " + o.toString());
}

ConstTest(String str) {
System.out.println("String: " + str);
}

public static void main (String[] args) {
new ConstTest("A");
new ConstTest(1);
new ConstTest(new Double(1.7));
}

}

"new ConstTest("A");" matches both Object and String. Since String is
a sub-class of Object, does that mean that the String constructor
always matches first?
 
R

raphfrk

On 2/14/11 11:22 PM, (e-mail address removed) wrote:
*There may
   be more than one such method, in which case the most specific
   one is chosen. The descriptor (signature plus return type) of
   the most specific method is one used at run time to perform
   the method dispatch.*

(*emphasis mine*)

If you find a non-compliant compiler, you should file a bug.

Pete

Thanks, was just wondering if it would be consistent.
 
M

Mike Schilling

Thanks, was just wondering if it would be consistent.

One of the design goals of Java is to be consistent across all compilers and
JVMs. If you find a situation where, say, the choice among method overloads
is inconsistent, you've found a bug in at least one of the compilers (or, I
suppose, the spec).
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,981
Messages
2,570,188
Members
46,731
Latest member
MarcyGipso

Latest Threads

Top