Thursday, August 25, 2005

Like in C and C++, one uses header files to access functions and classes outside the main file, in Java one uses packages. One uses the import statement to call the classes from the package. The classes have to be declared as public in the package to make sure that accessibility is possible in the class where it is being called. The .java files inside a package has a statement for package declaration, i.e. “package ff”, here the name of the package is ff. To make sure that the package classes are accessible outside the classes they have to be present in the CLASSPATH. CLASSPATH is an environment variable for setting the path for all possible locations where java class files can be present. If one needs to import any class from any package then one need to write the import statement. For eg. “import ff.*;” statement will import classes from the package ff.

If there is a directory named ff

Under this directory there is a file abc.java

package ff;

public class abc {
public abc() {
System.out.println("Inside class abc");
}
}


Another java file is created named a1.java

import ff.*;

class a1 {
public static void main(String args[]) {
abc a = new abc();
}
}

The file a1.java when executed will print "Inside class abc" if it finds the ff directory in its classpath.

No comments: