Tutorial Java SCJP – #13 Packages and Class access modifiers

Two important concept of Object Oriented Programming is to separate entities based on their functionality or logic and to hide data and behavior within a class (encapsulation). For that you have classes used to define entities that have attributes and a behavior (their methods). At a higher level, for a good organization of the application modules you can use packages to separate the work of different programmers or to aggregate classes in modules based on their logic or utility.

Other topics that are part of this Java tutorial are accessible through Java 6 Tutorial – Contents.

Packages are very important because allows different programmers to name their classes independently. For example, each programmer can define its own Utility class without generating a collision compiler error (the compiler won’t know which Utility class to instantiate): duplicate class.

The recommended naming convention for packages is reverse domain names, appended with division and project name. For example, I can name a package for a Chat application like this

package eu.itcsolutions.chat.client;

Other important rules regarding packages you need to know for the SCJP certification:

  • the package statement must be the first line (before import statements) in the file
  • if you want to use classes from other package you must specify the package name using import statements
  • import statements are defined between the package declaration (if any) and the first class declaration
  • if you want to use only one class from another package you define the import like this:
    import packageName.className;
  • if you want to use all classes from another package you define the import like this:
    import packageName.*;

The TestClass.java file:

package eu.itcsolutions.test;

public class TestClass {
    int attribute;
}

The Main.java file:

package eu.itcsolutions.main;

import eu.itcsolutions.test.*;

public class Main {
    public static void main(String[] args) {
        TestClass tc = new TestClass();
    }
}

What is class access

The Java platform implements tight security through bytecode verifiers, class loaders and security managers. One way to implement security at source code level is to control class access, meaning:

  • the class is visible in other packages;
  • you can use the class to create instances in other classes from the same/other package;
  • you can extend the class in a new one in same/other package;
  • you can access attributes or methods from the class (here you must take into consideration also attributes and methods access modifiers) in other classes from the same/other package;

What are class access modifiers

Class modifiers are:

  • default (when you don’t use anything); it’s equivalent to the package level access because the class is seen only by classes within the same package in any source file;
  • public meaning that the class is visible anywhere (in any package, in any source file);
package p1;

//use public access
public class Class1
{}

//use default access
class Class2
{}

and the relation between different packages is:

Java Class Access Modifiers
Java Class Access Modifiers

Other topics that are part of this Java tutorial are accessible through Java 6 Tutorial – Contents.