+ All Categories
Home > Documents > Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for...

Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for...

Date post: 02-Apr-2015
Category:
Upload: raphael-kille
View: 218 times
Download: 0 times
Share this document with a friend
23
Java Classes How does it find them?
Transcript
Page 1: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Java Classes

How does it find them?

Page 2: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Basic Approach

• Different from c++.– Uses header files for prototypes– uses INCLUDE variable to indicate location

of .h files not in system directories– libraries are stored elsewhere and located by

LIB variable

• Java uses only the java source.– No prototypes– uses a more complex naming/search scheme– details follow

Page 3: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

C++

#include “myclass.h”

void main ()

{ myclass x;

…..

}

public class myclass()

{ int a;

int b;

public:

myclass();

~myclass();

…..

}

myclass.cpp

myclass.h

How does c++ find theheader files? .. Searching the “include” path.

Page 4: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

No “include” in Java!

Only one file!

Page 5: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Java connects files with“package” and “import”

package •Groups class definitions together for privileged access, lets other classes in the package see more details •Generally a way of grouping a set of library routines

import •Very similar to include, but there is no include file counterpart •Identifies all packages OTHER THAN the one that class is in, from which this class needs access

Page 6: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Directory location is important

• Classes in the same package are stored in directory hierarchies to help import find the files.

• These directories also aid in resolution of ambiguous naming problems.

• More later….

Page 7: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

package first;

class classB { // most of your code}

package first;

class classA { classB b = … new classB();}

Two classes defined to be in package first

Being declared in the same package will allow each class to access each other without extra work.

classA using classB

classA.java

classB.java

Page 8: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

package second;

class classB { // most of your code}

package first; import second;class classA { classB b = … new classB();}

Two classes in different packages!

Being declared in the different package will require the use of import to access each other.

classA.java

classB.java

Page 9: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Package names• Can be dotted to show directory organization

• Example– math.algebra.sets– math.calculus.integration– math.calculus.differentiation

math

algebra calculus

integrationsets differentiation

import math.*; orimport math.algebra.sets;

Page 10: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Example Format of a filepackage com.myplace.mygroup.myclass; // OPTIONAL

import java.io.*; // OPTIONALimport anotherclass.mathlib.vectors; // OPTIONAL

public class thisclass { // most of your code}

So let’s examine this one element at a time.

Page 11: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

package com.myplace.mygroup.myclass; // OPTIONAL

import java.io.*; // OPTIONALimport anotherclass.mathlib.vectors; // OPTIONALpublic class thisclass { // most of your code}

•Provides a solution to the problem of generating a unique package name•Groups classes for more convenient use of related classes.•Defines the package in which a class is placed•Helps to define the directory in which the compiled file will be placed•Only ONE package statement per file because a class can only be in a single package.•If no package, class is considered to be in default package.

Page 12: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Unique package names• Lots of programmers in the world

• Significant opportunity for redundant naming of classes

• General principle -> namespaces

• Here, classes are grouped in packages to provide a means to give uniqueness to name– two programmers name the routine x

• com.mycompany.billy.io.x

• com.mycompany.sam.io.x

– x can be uniquely identified by qualification

Page 13: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Package name -> Directory name• A package name dictates the directory

structure in which class is stored.

• Does NOT indicate the complete path but a path that exists somewhere.

• E.g., com.mycompany.billy.io.x/

bin home ?

com

company

billy

iox

iox

sam

It DOES provide a unique name to usein accessing x!

Page 14: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

package com.myplace.mygroup.myclass; // OPTIONAL

import java.io.*; // OPTIONALimport anotherclass.mathlib.vectors; // OPTIONALpublic class thisclass { // most of your code}

•Directly related to packages•Used to provide a shorthand•Used for accessing OTHER classes NOT related to the package statement (if it exists in the file)•CAN have import without package and vice versa.•While they are related, the use of the use of them in the SAME file is typically independent/coincidental•package defines the package THIS class is IN•import used to ease USE of classes in OTHER packages

Page 15: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

import just makes is easier!

• The names have a tendency to be long in order to be unique

• import lets you use a shorthand for qualification.

• Like “with” in pascal to avoid listing all substructures in a record to access names.

• Example follows:

Page 16: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

How to define the CLASSPATH

/

bin home develop

com

company

billy

iox

iox

sam

Using a unix example,the code references “com..”In order to reference com.company.billy.io.xthe CLASSPATH must include /dev, everythingup to but not includingcom.

CLASSPATH= /lib/java;/develop

Page 17: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

What can one import access?import examples

company

billy

io

x

com

y

logoorganization

red - directoriesblack -> classes

// can’t access ANY classesimport com;

// can access // logo and organizationimport com.company.*;

// can’t access ANY classesimport com.*;

// can access only x import com.company.billy.io.x;

Page 18: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Which x?package com.myplace.mygroup.myclass; // OPTIONAL

import com.mycompany.sam.io.x;

public class thisclass { // most of your code x thisx = new x;}

Here x can be referenced in shorthand and is uniquelydefined to be the x in the io package defined by sam.

This also emphasizes disconnect between the packagein the package statement and the package in the import.

Page 19: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

What about * in import?

package com.myplace.mygroup.myclass; // OPTIONAL

import com.mycompany.sam.io.*;

public class thisclass { // most of your code x thisx = new x; y thisy = new y;}

* indicates that you can shorthand any class in the package.

In this example, y is considered to be in the same io packageand can be accessed in the same manner as x (shorthand).

Page 20: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

So how does java figure outEXACTLY where to put package?

• We used a ‘?’ in directory to indicate it was not known where.

• Determined at compile time

• uses -d option to say where

• E.g.– javac -d /project1/devstuff thisclass

– creates the package and the com.mycompany… directory under /project/devstuff

– /project/devstuff/com/mycompany/mygroup/...

Page 21: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Now how does it find the class when it compiles the program?

· 1. Look in java system class directories

· 2. Look in extensions directory (???)

· 3. Look in CLASSPATH.· Environment variable· default is current directory· unix example: .:/project/dev/java:/home/dgame/611

· current directory first

· then /project/dev/java

· and lastly /home/dgame/611

Page 22: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

Confusion• Search of path is documented in

contradicting manner

• O’reilly Java in a Nutshell:– “The interpreter always appends the location of

the system classes to the end of the path specified by the environment variable.”

• other references indicate it searches in the system first.

• Regardless, there can be no naming ambiguity

Page 23: Java Classes How does it find them?. Basic Approach Different from c++. –Uses header files for prototypes –uses INCLUDE variable to indicate location.

What if there are 2 classes with the same name?

• Compiler will alert you if there are multiple names in the path AND no means to resolve the ambiguity (import) is coded.

• Using import resolves which one you are referencing.

• If error message occurs, use more specific reference to the class.


Recommended