www.delorie.com/gnu/docs/gcc/gcj_25.html   search  
 
Buy the book!


Guide to GNU gcj

[ < ] [ > ]   [ << ] [ Up ] [ >> ]         [Top] [Contents] [Index] [ ? ]

10.2.1 Leaving out package names

Always using the fully-qualified name of a java class can be tiresomely verbose. Using the full qualified name also ties the code to a single package making code changes necessary should the class move from one package to another. The Java package declaration specifies that the following class declarations are in the named package, without having to explicitly name the full package qualifiers. The package declaration can be followed by zero or more import declarations, which allows either a single class or all the classes in a package to be named by a simple identifier. C++ provides something similar with the using declaration and directive.

In Java:

 
import package-name.class-name;

allows the program text to refer to class-name as a shorthand for the fully qualified name: package-name.class-name.

To achieve the same effect C++, you have to do this:

 
using package-name::class-name;

Java can also cause imports on demand, like this:

 
import package-name.*;

Doing this allows any class from the package package-name to be refered to only by its class-name within the program text.

The same effect can be achieved in C++ like this:

 
using namespace package-name;


  webmaster     delorie software   privacy  
  Copyright 2003   by The Free Software Foundation     Updated Jun 2003