What is a Package?

A package in Java is a mechanism to contain classes, sub packages or interfaces. They are used to prevent naming conflicts and for controlling access in addition to make searches and utilisation of classes, enumerations, interfaces and annotations easier. It makes things easier for other programmers as they can easily locate related classes.


Let's see, we have a class Student which has a property, age(i.e. variable )

  • When we write a code in a file and place it in some folder, we can have any number of files in it. Now imagine that a code is released to production and some issues occurred that need to be fixed. For this we need to find the java class in that folder where we have lot of files, which may take time to locate and further act on the bugs. If we want the code to be organised and maintained, we must have a proper folder structure, which is possible through the use of packages.

  • Maintenance - If any developer has newly joined a company, he can easily reach the files needed.

  • Reusability - A common code can be placed in a common folder so that everybody can check that folder and use it.

  • The Package concept uses two keywords: package and import.

  • The syntax for package is:

    • package com.tcs.icici.loan.homeloan.emi.penalty

    • package com.tcs.icici.loan.homeloan.emi.advance

    • package com.tcs.icici.loan.carloan.emi.advance

    In the above example, the folder starts with ‘com’. This is generally a company specification. In this case, the root folder is 'com'.
  • Then we have subfolder 'tcs'.This is the name of the company in which product is developed.

  • icici is the client name for which we are developing our product.

  • loan is project name.

  • home loan and car loan are loan types, and so on.

  • Let it be noted that we can give any names, this is just a specification. If we write Package com.hi.hello, it means ‘com’, ‘hi’ and ‘hello’ is the folder structure.

  • Keep in mind that the root folder should always be same for all classes.

Write classes as below:

java classes example

  • Now, we have multiple classes as shown in above diagram, 4 classes A, B, C and D.

  • I am now modifying class A as below, so that we can observe how to run a program with packages,

    package com.hi; 
    class A {
        public static void main(String as[]){
        System.out.println ("javabykiran is good institute !!!!! ");
       }
    }

Remember:

  • Package statement can be written at the start of the file only once.

  • Start with root folder and ends with sub-directory name only, not the file name.

  • Always compile with –d option. If you are compile from command prompt.

  • javac -d .*.java

      Here,
    • -d means directory

    • . [dot] means current directory where you want to place com folder after compilation

    • *.java means all java files will get compiled

When we want to use one class in another class we must use the import statement. Here, you don’t need to refer to the whole package name, but simply refer to classes that are declared in different packages.

After modifying class A from above [ added a line C c=new C() ]


To give java class file name, always find the root folder first and then go down to the class file. In this case, to write ‘C’ class name first find class A's root folder that is com, then traverse to required Class file that is C here.
We must write the following:

    com. hello.C c=new com.hello.C();

Instead of, this we can use import the code looks like this:


Here, it's been seen that the readability of code decreases if we write a long path of class in a code. So, what Java says is, go for an import statement like given in the example below. It’s one and the same thing, but we should remember both ways.

Thus, to maintain readability of code, Java provides another way i.e. import keyword.

The Code will look like this:


When you compile it, it will surely compile. It’s same as earlier example using dot, but the readability of the code is increased. If you want to compile it all, then mark all classes as public.


Question.:

You may be asked by interviewer why are you using two ways? And why can’t you do everything by a simple import statement. Have you seen any scenario where com.hello.C cc=new com.hello.C () is used?

Answer.

Take a look at this scenario:

java package example

The Code will look like this:


Will the above code compile?

    No, because line number three does not say anything about class A from which package (hi or hello) it is being referred from.

    No, because line number five also does not say anything about class A from which package (hi or hello) it is being referred.

    No, because line numbers four and six will get confused about which package's class needs to pick here. Therefore, we now have a confused compiler. So, there are many reasons for this.

    In this case import is not working, so we will go for a different method, way dot way i.e. using the qualified/full fledge path.


Remember these points while using import statement in compiler perspective:

  • Import can be written multiple times after the package statement and before the class statement.

  • It must be start with the root folder name [No subfolder name] and the file name must end with semicolon.

  • import com.hi.A; → Correct.
    import com.hi.*; → Correct, all files from hi folder imported.
    import com.hi;   → Wrong, it gives the error that hi file not found in com folder.

  • import does not mean that the memory is allocated, it just gives a path to reach, many factors which later decide whether you can create object or not.

  • import com.hi.A; is always better than import com.hi.*;
    In industry, the code reviewer may reject your code if we write
    import com.hi.*;