Header Ads

Packages in java

Packages in java



Package in java is an encapsulation mechanism to group related and interface into a single module.


The main objective of packages are:

1. To resolve name conflicts.


2. To improve modularity of the application.


3. To provide security.


4. There is one universally accepted naming conversion for packages that is to use internet domain name in reverse.


Example:


    com.hdfcbank.loan.housingloan.Account

    Here, com.hdfcbank: client internet domain name in reverse

              loan:         module name

              housingloan:  submodule

              Account:      class



How to compile package Program?


Example 1:


package com.pingjavajobs.itjobs;

class SiliconJobs

{

  public static void main(String args[])

  {

    System.out.println("package demo");

  }

}


Javac SiliconJobs.java generated class file will be placed in current working directory



Note 1:

         cmd-------->SiliconJobs.class


1. javac -d . SiliconJobs.java (run on cmd)

2. -d means destination to place generated class files "." means current working directory

3. Generated class file will be placed into corresponding package structure



Note 2:

 com -----> pingjavajobs ------> itjobs-->;SiliconJobs.class


1. javac -d c: SiliconJobs.java

2. If the specified package structure is not already available then this command itself will create the required package structure.

3. As the destination we can use any valid directory.

4. If the specified destination is not available then we will get compile time error.




Note 3:

  com ------> pingjavajobs ------> itjobs -----> SiliconJobs.class


1. javac -d c: SiliconJobs.java

2. If the specified destination is not available then we will get compile time error.

3. If it is not available then we will get compile time error.




How to execute package Program?

1. java  com.pingjavajobs.itjobs.SiliconJobs

2. At the time of execution compulsory we should provide fully qualified name.


Conclusion 1:


In any java Program there should be at most one package statement that is if we are taking more than one package statement we will get compile tine error.


Example:


package pack1;

package pack2;

class A

{


}


Output:

        Compile time error




Conclusion 2:


In any java Program the 1st non comment statement should be package statement[if it is available] otherwise we will get compile time error.


Example:


import java.util.*;

package pack1;

class A

{

 

  

}


Output:

       Compile time error



Note 4:

An empty source file is a valid java Program.




Java Source File Structure:


1. In java source file structure,package statement is at most one.

2. In java source file structure,import statements can be more than one or any number is allowed .

3. In java source file structure,class/interface/enum declarations can be more than one or any number is allowed.

4. In java source file structure,order is important and so order is package statement and then import statement and then comes class/interface/enum declarations. 



An Inspirational quote: 

"Everything you can imagine is real."
 


Steps to Subscribe my blog in 15 secs:
1. Enter your mail id in "Subscribe" box.
2. Enter Captcha for verification.
3. A mail will be received with link in your mail id.Just Click on link  and  then Done .Thank you for subscribing :)



Happy Reading.Happy Learning!!! See you in next post  :)

No comments

Note: Only a member of this blog may post a comment.