Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Which import should be considered?

Consider the following code snippet.

package spoon;

import rx.Observable;
import java.util.*;

public class Test {
    public void test() {
        Observable o = new Observable();
    }
}

This code compiles well with Java 11 even though I thought it should not. Observable is also a java class in java.util and I do not understand how the compiler knows whether to use Observable from java.util or my custom package rx.

Note that java.util.Observable was deprecated in Java 9, however, I don’t think so it should have any effect on imports.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

The single name import wins over the wildcard (called "import-on-demand" by the standard).

To cite chapter and verse:

https://docs.oracle.com/javase/specs/jls/se11/html/jls-7.html#jls-7.5.2

"The declaration might be shadowed by a single-type-import declaration of a type whose simple name is…"

https://docs.oracle.com/javase/specs/jls/se11/html/jls-6.html#jls-6.4.1

"A type-import-on-demand declaration never causes any other declaration to be shadowed. "

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading