Packages ,Access Levels, Public

We use packages in order to avoid naming collision and also to organize code according to task.
Type of Constructor

In the above example we have created two java sources files in src directory. Both files contain class B in them. We cannot compile these files using *.java since compiler will go in ambiguity because of multiple definition of class B. Thus to avoid these types of naming collision we use packages.

Package Flow Diagram

                    package  pack1;
                    class A
                       public static void main(String []args)
                          System.out.println(“Hello World”);

We declare package structure of a java file using package keyword. Package keyword must be the first line of our source file always. when we compile above source file using –d option, class files will be created in classes folder with the package structure as define using package keyword. since we have declared package as pack1, .java file must be saved inside pack1 folder.pack1 will now qualify as package. Now class file of class A is a part of package pack1.

During compiling we must use
   E:\LAB\sdjbatch\package-dev\src\javac –d ../classes pack1/
   While running
   E:\LAB\sdjbatch\package-dev\classes\java pack1/A
   E:\LAB\sdjbatch\package-dev\classes\java pack1.A

Now let us create package stream as shown.
Create Package Stream

  In the above program both class A and class B belong to same package pack1. Class B shares a HAS- A relation with class A. Thus class B is using class A for package. Here we have class A. Thus class B is using class A for object creation. By default compile will search for class A in the present package. Here we have class A in same package. Thus the program will compile and run successfully. and share a common package stream i.e com.pack1

Access Specifiers:

  Access Specifiers are the keywords which specify the accessibility of class and class members. In java we have four access levels and three access Specifiers.

    The four access levels are
    1. Accessibility within same class.(Private)
    2. Accessibility within same package.(Default)
    3. Accessibility with same package and sub class of other package.(Protected)
    4. Accessibility from anywhere.(Public)

    Private :  if we use private to class members as access specifier, we can access those members only within the same class.

    Default :  in default access we don’t specify any access specifier.In case of default access we can access those members within the package only.

    Protected :  if we use protected to class members as specifier, we can access those members within same package as well as within inherited class present in other packages.

    Public :  if we use public to class members as access specifier, we can access those members from anywhere.

    We know that when we use any class inside other by default compiler will search that file with in the same package.

Type of Constructor

Type of Constructor

  In the above program we are creating source files in different packages. Class B is having HAS-A relation with class A. we cannot compile different package classes at once. We need to be inside are directory to compile will check for class A in the default package for resolving dependency. but class A is in other package but in same stream. To specify where class A is in the current stream, we use import keyword. We must use import keyword after define package. Now compile will not check present package but it will go to the specified package in order to use class A. when we compile, compiler will produce A. class first and then B.class since dependencies will be resolved first.

  One important point to remember here is that whenever we are accessing a class and class member outside the package, both class and class member should be declared as public as shown in above program. When we define package stream in a source file it applies to all classes defined within that source file.

Now let us create source files in different stream within same drive (classpath).
Different stream within same drive

Type of Constructor


 Inside A(when we set classpath)

  In the above example we have source files in different stream.For compiling only import statement of class A is not enough since it will not show complete path of class A to class B.

  In order to compile successfully we need to set CLASSPATH environmental variable to show the location of class file of compiler will treat class A as third party class while compiling class B.

  Class path environment variable is just for .class files and not for .java files. Hence we can only tell location of .class file through classpath .thus we need to compile class A first and generate .class file to class A and set its CLASSPATH. During compiling class B,if there is any requirement of third party class(i.e. class A),compiler will check for that class in the present stream .if it is not finding then it will check CLASSPATH environment variable for the path of third party class. If it finding then it will check CLASSPATH environment variable for the path of third party class.if it finds third party class there then it will compile class B successfully.

If we compile class A using normal way i.e

 E:\LAB\sdjbatch\package-dev\dev1\src>javac –d ../classes pack1/ This will produce A.class file in classes folder of dev1.

In the same way if we try to complie from src of dev2,

 E:\LAB\sdjbatch\package-dev\dev1\src>javac –d ../classes pack2/

This will not compile since compiler is not to resolve dependency i.e. it is not able to get class A. thus we need to set classpath to tell compiler where A. class is located.

To set classpath for single command execution we can use command as.

 E:\LAB\sdjbatch\package-dev\dev2\src>javac –d ../classes –classpath .../../dev1/classes pack2/

Now compile will identify where A.class is and it will use for syntax checking and compilation will succeed.

Setting classpath:

    We can set classpath in many ways.
    1 ) Command wise as shown above.
    2 ) Command window wise, by using set command.
    Ex: set classpath= < classpath>
    3 ) Globally through
     My Computer->properties->advanced->Environment variable.

      E:\LAB\sdjbatch\package-dev\dev2\classes>java pack2.Manager
      O/P  :   NoClassDefFoundError : pack1/A

    Here java command is searching for third party class A while running class B and it is not able to find third party class. Hence the error.

    E:\LAB\sdjbatch\package-dev\dev2\classes>java –classpath ../../dev1/classes pack2.Manager

    O/P  :   NoClassDefFoundError : pack1/Manager

    Here java command is trying to execute pack2.B from class path location.But in the classpath there is no pack2.B. Hence the above error.

    E:\LAB\sdjbatch\package-dev\dev2\classes>java –classpath ../../dev1/classes;. pack2.Manager

    Here, .represent present directory.