Packages

Packages

                                              Packages

It is encapsulation mechanism to group related classes and interfaces into a single unit, which is nothing but a package.

eg-:
1-:All classes and interfaces which is required for database operations are grouped into a single package which is nothing but a java.sql package.
2-:all classes and interfaces which are used fully for file io operations are grouped into a separate package which is nothing but the java.io package.



The main advantages of the package are-:
  • To resolve name conflict that is a unique identification of our component
  • it improved the modularity of the application 
  • it improves the maintainability of the application.
  • it provides security for our components.
There is one universal naming convention for packages which is to use an internet domain name in reverse.

eg-: com.icicibank.loan.housing.Account;

com.icicibank-: client's internet domain name in reverse.
loan-:module name.
housing-:sub module name.
Account-: class name.

package com.code.java;
public class Test{
  public static void main(String[] args){
     System.out.print("hello");
  }
}

javac Test.java 
Generated .class file will be placed in the current working directory.



javac -d . Test.java

-d -: destination to place generated .class file
(.)-: CWD(current working directory)

  • generated .class file will be placed in corresponding package structure.

  • if the corresponding package structure is not already available then this command itself will create the corresponding package structure.
  • as destination instead of dot we can take any valid directory name example javac -d F: Test.java
  • if the specify the directory not already available the we will get compile time error .
                eg-: javac -d Z: Test.java 
  •      if z : not available then we will get compile time error saying directory not found Z:

  •      at the time of execution we have to use fully qualified name
                  eg-: javac  com.code.java.Test


Conclusion-:
  • In any java source file there can be almost one package statement that is more than one package statement not allow otherwise we will get compile time error.
        eg-:
                package pack1;
                package pack2;
                public class Test{
                    public static void main(String[] args){
                        System.out.print("hello");
                    }
                }
compile time error saying -:class, interface or enum expected.

  • in any java program the first non-comment should package statement (if it is available) otherwise we will get compile time error.

                    eg-:
                     import java.util.*
                     package pack2;
                     public class Test{
                            
                     }
                    compile time error saying -:class, interface or enum expected.

the following is valid java source file structure .
  • package statement
  • import statement
  • class name or interface or enum declaration 
note-:an empty source file is a valid java program hence the following are valid java source files.