Question:
What's the purpose of import in Java?
JerZyNoVa
2015-02-24 07:28:52 UTC
As I noticed for some programs it isn't used and some it is.

For Example:
public class HbdMom
{
public static void main(String[] args)
{
final String MSG1 = "********************";
final String MSG2 = "***Happy Birthday***";
final String MSG3 = "******* Mom! *******";
final String MSG4 = "********************";
System.out.println(MSG1);
System.out.println(MSG2);
System.out.println(MSG3);
System.out.println(MSG4);
}
}

Nothing is imported.

But for:
import java.util.Scanner;

public class Plp02A
{
public static void main(String[] args)
{
int i = 0;
int[] lab02aArray = { 87, 101, 108, 99, 111, 109, 101, 32, 44, 32, 116, 111, 32, 67, 83, 67, 32, 49, 48, 49, 33 };
Scanner strIn = new Scanner(System.in);
System.out.print("Please type your name...");
String stdName = strIn.nextLine();
System.out.println("\n\n");
for (i = 0; i < 8; i++)
System.out.print((char)lab02aArray[i]);
System.out.print(stdName);
for ( ; i < 20; i++)
System.out.print((char)lab02aArray[i]);
System.out.print((char)lab02aArray[i] + "\n\n");
System.out.println("**** end of Lab02a ****");
} // end of main method
} // end of class Lab02a

java.util.Scanner is imported.


*Please keep in mind this is all new to me, as I just began taking classes in Java this semester.

I'm just wondering when would you would need to import something when writing a java program.
Three answers:
husoski
2015-02-24 07:59:08 UTC
In Java, the import statement simply lets you use public classes from another package without using the package name as a prefix.



The Scanner class is in the java.util package, so you could use:



java.util.Scanner strln = new java.util.Scanner(System.in);



...and that will work without an import statement.



This form will let you mix code from different sources that might have used the same name for a public class. Suppose you have installed a set of programs for radio communications that has a Scanner class. That might be spiffy.radio.policeband.Scanner or something like that.



If Java ever has a choice of two or more versions of a class (suppose you imported both java.util.* and spiffy.radio.policeband.* and used just plain Scanner) then that's an error. The compiler will issue a message and the program won't run. You can only use imported shorter names when there's one and only one class name.



That last bit is a reason for avoiding .* imports, and listing every class you intent to use in its own import statement. You are less likely to have your program suddenly fail to compile because some future version of Java added a public class that duplicates one of your class names.
?
2015-02-24 07:54:36 UTC
What SteveO said, plus...



If you want to use Scanner in your program, you need to inform the compiler that it needs to (import) look up the relevant utilities part of the Java API.



Also, if you had your java program in two or more separate folders, you would need to import the other file(s) to your class with the main method.
SteveO
2015-02-24 07:54:29 UTC
System.out.println is contained in java.lang.* and does not need to be explicitly imported since it's more or less automatically imported regardless. Anything that isn't in java.lang.* needs to be explicitly imported.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...