Java organization of classes.

Java organization of classes

  • It's normal to arrange java classes in packages to separate classes from each other given the type of data they handle.
  • If you study Java API library you will see that all the classes are arranged inside the packages.
  • A class is declared to belong to a particular package with the package statement.
  • There can be only one package statement in a java file and the statement should be the first in the java file.
    Java Package notation example:
    package mytools.text;
    class TextComponent {
     ...
    }
    ...
  • Package names are constructed hierarchically, using a dot-separated naming convention.

Java Importing Classes

By default, a class is accessible only to other classes within its package so classes you need from other packages must be imported.
You can refer directly to classes in other packages by its fully qualified name:
package somewhere.else;
 class MyClass {
      mytools.text.TextEditor editBoy;
        ...
    }
...
Or using import statement:
package somewhere.else;
import mytools.text.TextEditor;
class MyClass {
    TextEditor editBoy;
    ...
}
...
Or import all the classes in a package using the * wildcard notation:
package somewhere.else;
import mytools.text.*;
class MyClass {
    TextEditor editBoy;
    ...
}
...
You can import static members of a class into the namespace of your file:
import static java.lang.Math.*;
// This will import all static Methods from java.lang.Math class 
...
    // usage
    double circumference = 2 * PI * radius;
    double length = sin( theta ) * side;
    int bigger = max( a, b );
    int positive = abs( num );
...
© 2010 by Finnesand Data. All rights reserved.
This site aims to provide FREE programming training and technics.
Finnesand Data as site owner gives no warranty for the correctness in the pages or source codes.
The risk of using this web-site pages or any program codes from this website is entirely at the individual user.