¿Qué hace Java cuando importamos paquetes varias veces en la misma clase?
Frecuentes
Visto 1,645 veces
5
Try this piece of code. It compiles.
import java.io.*;
import java.io.*;
import java.io.*;
import java.util.*;
import java.util.*;
import java.util.*;
import javax.swing.*;
import javax.swing.*;
import javax.swing.*;
public class ImportMultipleTimes
{
public static void main(String[] args)
{
System.out.println("3 packages imported multiples times in the same class.");
}
}
Does the compiler simply ignore the additional import statements?
1 Respuestas
13
Yes, it will be considered redundant by the compiler, as specified by the JLS 7.5.2:
Two or more type-import-on-demand declarations in the same compilation unit may name the same type or package. All but one of these declarations are considered redundant; the effect is as if that type was imported only once.
Nota:
- "type-import-on-demand" is a package import:
import somepackage.*;
- lo mismo aplica for single-type-import : "If two single-type-import declarations [...] attempt to import [...] the same type, [...] the duplicate declaration is ignored."
Respondido 28 ago 12, 15:08
No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas java or haz tu propia pregunta.
si it will considered redundant by the compiler - Sundar G
How did you come to wondering this? - John Dvorak