Presentation is loading. Please wait.

Presentation is loading. Please wait.

© 2001 by Ashby M. Woolf Revision 2 A First Look at CLASSPATHs, Packages and Import Statements How a Java Class Locates The Other Classes it Needs.

Similar presentations


Presentation on theme: "© 2001 by Ashby M. Woolf Revision 2 A First Look at CLASSPATHs, Packages and Import Statements How a Java Class Locates The Other Classes it Needs."— Presentation transcript:

1 © 2001 by Ashby M. Woolf Revision 2 A First Look at CLASSPATHs, Packages and Import Statements How a Java Class Locates The Other Classes it Needs

2 © 2001 by Ashby M. Woolf Revision 2 Rule #1 A file may define any number of Java classes. Only one of the defined Java classes can be a public class. (Class order doesn't matter) Example: class One { // Class One stuff } public class Two { // Class Two stuff } class Three { // Class Three stuff }

3 © 2001 by Ashby M. Woolf Revision 2 Rule #2 The name of the a file defining one or more Java classes must be the name of the public class (if there is one) followed by.java Example: class One { // Class One stuff } public class Two { // Class Two stuff } class Three { // Class Three stuff } must be in a file named Two.java

4 © 2001 by Ashby M. Woolf Revision 2 Rule #3 If I compile a Java source code file ".java" get a class file for every class in the file Example - Compiling: class One { // Class One stuff } public class Two { // Class Two stuff } class Three { // Class Three stuff } Produces the class files: –One.class –Two.class –Three.class

5 © 2001 by Ashby M. Woolf Revision 2 Observations Java can find all classes by looking at file names Looking for.class files everywhere will not work Where should Java look?

6 © 2001 by Ashby M. Woolf Revision 2 What is Needed We want to be able to store collections of classes (.class files) in one or more trees that make sense. We want to be able to work with several collections of classes and move them about when necessary. We want each class to be able to specify where to look in a collection (tree) to find the other classes it wants to use. We don't want to change our program (class) if a collection (tree of.class files) is moved.

7 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH CLASSPATH is an environment variable that specifies the location to look for the top of a tree expected to contain.class files Example CLASSPATH=C:\b;C:\d\m Java would first look in the directories below C:\b If the class being sought was not there it would look in the directories below C:\d\m

8 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH Cont. We can store different libraries of.class files where we need to put them and just add them to the CLASSPATH. We can move Libraries and change the class path to suit. We can run different versions of libraries by changing the CLASSPATH

9 © 2001 by Ashby M. Woolf Revision 2 Why the import Statement? Don't want to look in every directory in a large library –May find the a class with the same name in two places –Waste lots of time wandering around looking –Have no way to merge names from separately developed libraries without changing some names For every directory in the CLASSPATH Java only looks in one subdirectory for the class file being sought.

10 © 2001 by Ashby M. Woolf Revision 2 A Fully Qualified Class Name Example public class Foo { com.ajax.Bar b = new com.ajax.Bar(); ) Running in an environment with a CLASSPATH=C:\d\m Java would look for the Bar class specifically in the directory C:\d\m\com\ajax ( for the file Bar.class)

11 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abcd ijlmn compq xajaxz Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m public class Foo { com.ajax.Bar b = new com.ajax.Bar(); } Fe. class Fi. class gov irs Bar.class javac

12 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n compq xajaxz Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m public class Foo { com.ajax.Bar b = new com.ajax.Bar(); } Fe. class Fi. class gov irs Bar.class javac

13 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m public class Foo { com.ajax.Bar b = new com.ajax.Bar(); } Fe. class Fi. class gov irs Bar.class javac

14 © 2001 by Ashby M. Woolf Revision 2 The import Statement The import statement are found at the beginning of a Java source file (a.java file). Example import com.ajax.Bar public class Foo { Bar b = new Bar(); ) Running in an environment with a CLASSPATH=C:\d\m Java would look for the Bar class specifically in the directory C:\d\m\com\ajax ( for the file Bar.class)

15 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ a b c d ijgovl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class javac irs Bar.class

16 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n compq xajaxz Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class gov irs Bar.class javac

17 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class gov irs Bar.class javac

18 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ a b c d ijgovl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class javac irs Bar.class

19 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ a b c d ijgovl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class irs Bar.class javac

20 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ a b c d ijgovl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class irs Bar.class javac

21 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n compq xajaxz Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class gov irs Bar.class javac

22 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m import com.ajax.Bar public class Foo { Bar b = new Bar(); } Fe. class Fi. class gov irs Bar.class javac

23 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m Fe. class Fi. class gov irs Bar.class javac import com.ajax.* public class Foo { Bar b = new Bar(); }

24 © 2001 by Ashby M. Woolf Revision 2 What is a Package All the class files in the same directory called a package or or said to be part of the same package. When we write: –import com.ajax.* –We say we have imported the package com.ajax

25 © 2001 by Ashby M. Woolf Revision 2 CLASSPATH C:\ abc d ijl m n com pq x ajax z Bar.class Ball.class Bell.class Foo.class CLASSPATH = C:\b;C:\d\m Fe. class Fi. class gov irs Bar.class javac import com.ajax.* public class Foo { Bar b = new Bar(); } The Package The Package Name

26 © 2001 by Ashby M. Woolf Revision 2 Keeping Libraries Separate Java programs are frequently stored in trees that begin with reversed domain name sequences. Example: –edu/tamu/math/util/* –edu/utexas/math/util/* Avoids conflicts

27 © 2001 by Ashby M. Woolf Revision 2 Imported by Default The package " java.lang.*" is imported by default Imports a minimal package of Java classes

28 © 2001 by Ashby M. Woolf Revision 2 A Simple Constraint com.ajax in the import statement expects the directory structure \com\ajax As a Result you can't have ". "s in the directory names of a tree of Java classes

29 © 2001 by Ashby M. Woolf Revision 2 Exercise Check your Sun Documentation and find out how many "Packages" are listed

30 © 2001 by Ashby M. Woolf Revision 2 End of Content


Download ppt "© 2001 by Ashby M. Woolf Revision 2 A First Look at CLASSPATHs, Packages and Import Statements How a Java Class Locates The Other Classes it Needs."

Similar presentations


Ads by Google