Java Import Statement Copyright 1997 2010 Curt Hill

  • Slides: 10
Download presentation
Java Import Statement Copyright © 1997 -2010 Curt Hill

Java Import Statement Copyright © 1997 -2010 Curt Hill

Import Statement • Gives access to existing classes and interfaces – Similar to #include

Import Statement • Gives access to existing classes and interfaces – Similar to #include of C • Usually first lines in file – Follows package, if present – Often follows comments • Example: import java. awt. Color; Copyright © 1997 -2010 Curt Hill

Import Form • import is a keyword • Following that is the class you

Import Form • import is a keyword • Following that is the class you want to have access to • Wildcards can be used: import java. awt. *; – Using wildcards may slow the compile by 10% and who cares? – Object code will usually be same Copyright © 1997 -2010 Curt Hill

Directories • Java grew up in a UNIX environment • Its import statement reflects

Directories • Java grew up in a UNIX environment • Its import statement reflects this tree shaped directory • Each dot represents a boundary between one directory and another • Thus the import java. awt. Color; is in directory: java/awt and the file is named Color. CLASS Copyright © 1997 -2010 Curt Hill

Tree Shaped Directories • The disk of most file systems contains two types of

Tree Shaped Directories • The disk of most file systems contains two types of files: – Directories – Files • Each directory may then also contain other files and other directories • First seen in MULTICS but popularized by UNIX • Appeared in DOS 2 and later Copyright © 1997 -2010 Curt Hill

JAR Files • Java also has its own archive file: JAR • JAR stands

JAR Files • Java also has its own archive file: JAR • JAR stands for Java ARchive • A JAR file has exactly the same format as a zip file – It may be maintained by zip utilities that allow different extensions • A JAR or ZIP file may also contain a directory structure • An import may reference the directory structure within a JAR file Copyright © 1997 -2010 Curt Hill

Common imports • Basics in Java. Lang need no import • Applets will usually

Common imports • Basics in Java. Lang need no import • Applets will usually use: import java. applet. *; import java. awt. *; • Applications may also use: import java. io; import java. net; import java. util; • Developed code may also be imported Copyright © 1997 -2010 Curt Hill

Example • The following picture shows a Windows C: disk – It contains several

Example • The following picture shows a Windows C: disk – It contains several directories and files • The Java runtime is in the Program Files directory • The runtime contains a JREn. jar • This contains where imports may be found, especially these: – import java. awt. Color; – import java. utils. Scanner; – import java. awt. *; Copyright © 1997 -2010 Curt Hill

Tree Shaped Directories C: Disk dir 1 … Programs … File 1 … Windows

Tree Shaped Directories C: Disk dir 1 … Programs … File 1 … Windows JRE File 1 java awt Color. class … utils JButton. class Copyright © 1997 -2010 Curt Hill … Scanner. class

Developed Code • World and Turtle are classes not part of the JRE •

Developed Code • World and Turtle are classes not part of the JRE • If incorporated into a JAR file they may be accessed with an import • Otherwise the IDE may be directed to make them part of the global scope Copyright © 1997 -2010 Curt Hill