30
© 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

© 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

  • View
    214

  • Download
    1

Embed Size (px)

Citation preview

© 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

© 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

}

© 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

© 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

© 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?

© 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.

© 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

• ExampleCLASSPATH=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

© 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

© 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.

© 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)

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j l m n

com p q

xajax 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. classFi. class

gov

irs

Bar.classjavac

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j l m n

com p q

xajax 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. classFi. class

gov

irs

Bar.classjavac

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j l m n

com p q

xajax 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. classFi. class

gov

irs

Bar.classjavac

© 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).

• Exampleimport 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)

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j gov l m n

com p q

xajax 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. classFi. class

javac

irs

Bar.class

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j l m n

com p q

xajax 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. classFi. class

gov

irs

Bar.classjavac

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j l m n

com p q

xajax 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. classFi. class

gov

irs

Bar.classjavac

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j gov l m n

com p q

xajax 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. classFi. class

javac

irs

Bar.class

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j gov l m n

com p q

xajax 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. classFi. class

irs

Bar.classjavac

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j gov l m n

com p q

xajax 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. classFi. class

irs

Bar.classjavac

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j l m n

com p q

xajax 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. classFi. class

gov

irs

Bar.classjavac

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j l m n

com p q

xajax 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. classFi. class

gov

irs

Bar.classjavac

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j l m n

com p q

xajax z

Bar.class

Ball.class

Bell.class

Foo.class

CLASSPATH = C:\b;C:\d\m

Fe. classFi. class

gov

irs

Bar.classjavac

import com.ajax.*

public class Foo { Bar b = new Bar();}

© 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

© 2001 by Ashby M. Woolf Revision 2

CLASSPATH

C:\

a b c d

i j l m n

com p q

xajax z

Bar.class

Ball.class

Bell.class

Foo.class

CLASSPATH = C:\b;C:\d\m

Fe. classFi. class

gov

irs

Bar.classjavac

import com.ajax.*

public class Foo { Bar b = new Bar();}

The Package

The Package Name

The Package Name

© 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

© 2001 by Ashby M. Woolf Revision 2

Imported by Default

• The package "java.lang.*" is imported by default

• Imports a minimal package ofJava classes

© 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

© 2001 by Ashby M. Woolf Revision 2

Exercise

• Check your Sun Documentation and find out how many "Packages" are listed

© 2001 by Ashby M. Woolf Revision 2

End of Content