Packages

Package ? What is that ? Any gift wrapped ? Any tourism package or what is that ?
Yes it is a gift from Java.
No, not a tourism package or such but the concept is similar.


Well, let's go by a simple example. let's say you are residing in Barddhaman and your name is Palash and a letter is there in post office with your name on it.

Now, the postman thinks, there might be thousands of 'Palash' residing in Barddhaman. Which one I should forward this one to ???

Well, he checks for one for time and finds that it has 'Palash Kanti Kundu' in the name field. Now he is a bit relaxed now. But again, he thinks that well, hndreds of 'Palash Kanti Kundu' is residing in Barddhaman, which one to forward this letter to.

Again he checks if there is mentioned anything else over there, well he finds, the following

Tikorhat, Hazidanga
P.O:- Lakurddi, Battala

Now, he is some more relaxed, he then forwards this to the corresponding post office. Again, in the corresponding post office, the postman thinks, there may be more than one 'Palash Kanti Kundu' residing in Tikorhat locality.

The postman now tries to find some more information. Well, at this time he finds the following,

House Number - 217

Discovering this information, he is quite relaxed that, in the house I am pretty sure to get one 'Palash Kanti Kundu' and he moves to that particular house and calls Palash, Palash comes up and receives the letter.

Nice dude, you got your letter but what is there to do with Java, we are here to collect information on Java and not  to get the information of how postman works ?
Hmm, it's good to hear that you are here to get some information on Java. But, would you like to think for a couple of minutes if you can relate it to Java ?

OK. Let me think again.


























Hey, doesn't it resolves the scenario when two people may have the same name ?
Yeah, it does the same you think. You got it. Think in Java style...
















Well, what if two classes are having the same name ?
You are on the track, a little bit more...










What if we can provide different house for them ?
Hmm, well you are correct but a more subtle answer would be to provide different address for them.







WHAT ? ADDRESS of a Class ????
Don't be surprised. We provide address to different Java classes and these addresses are known as Java Packages.

Sounds interesting...
Yup, it is.

Well, let's start of an example. Let's say your friend and you are working on an application. Where both of you need a specific type of Employee class. Let's say you are working on HR specific role and your friend is working with Development specific role. So, you write your Employee class who performs People Management and your friend writes Employee class which performs development stuffs.

Your friend has written the following Employee class,


/*
 * Copyright 2014-2015 Palash Kanti Kundu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
 * @author Palash Kanti Kundu
 * @version 1.0
 * @since Sep 6, 2015
 */
public class Employee {
 public void writeUnitTest() {
  // Unite test stuffs
 }

 public void writeCode() {
  // Coding stuffs
 }

 public void testModule() {
  // Module testing stuffs
 }

 public void integrate() {
  // Integration stuffs
 }

}

And you have written the following

/*
 * Copyright 2014-2015 Palash Kanti Kundu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


/**
 * @author Suman Kundu
 * @version 1.0
 * @since Sep 6, 2015
 */
public class Employee {
 public void recruit() {
  // Select candidates for organization
 }

 public void survey() {
  // Survey of existing employees
 }

 public void payroll() {
  // calculate employee salaries
 }

}

Both of you have performed all the testing required, all the modules are working fine, all the methods are behaving as perfect as it could be, same goes for your friend's stuff too. So, you both are happy and both of you decide to merge everything into a single piece named organization.
You start of merging.....



Ohh shit !!!

Every thing is lost and all the employees are working as developer, no HR management...

Where are the methods to perform recruitment, employee satisfaction management, payroll management. Everything only relates to coding, unit testing, integration, build, deployment etc.




What happened ?
You again start off writing Employee to do HR stuffs and integrate both set of codes.


All the steps performed. But where did the hell, development stuffs go ?
Only people management, payroll management, employee satisfaction survey etc stuffs are there.


Now both of you started worrying about it and thought the whole day, what is happening. But after loosing a whole day you thought of finding an expert and get his help.

The big Java Gurus are waiting out there and you just asked one of them for help. He listened to all your problems and came to visit your office and started exploring your code.

Within a minute or two he fixed the error, merged both of your codes and started the system and you found that magically everything works as perfect as you thought it to be.

Now, you are curious of the task he performed and started finding the change he did. You found that in both of your Employee classes, he wrote the following line on the top.


package com.ourcode.dev;

and


package com.ourcode.hr;

OK, I got it now. So, package is the address, the postman looks for. Am I right ?
Yep. You can tell so.

So, who is the postman in this context ?
Well, the almighty JVM does this for you. He employs ClassLoader to do this job. You don't have to worry about all these things right now. What really is required from a Development standpoint is to provide a correct and meaningful address to your classes.

Let's now try to define a formal definition,

Packages: A package is a technique for organizing similar kind of classes and can be very much useful in resolving naming conflicts.

Java itself is based on different set of packages,
java.lang— basic language functionality and fundamental types
java.util— collection data structure classes
java.io— file operations
java.math— multiprecision arithmetics
java.nio— the New I/O framework for Java
java.net— networking operations, sockets, DNS lookups, ...
java.security— key generation, encryption and decryption
java.sql— Java Database Connectivity (JDBC) to access databases
java.awt— basic hierarchy of packages for native GUI components
javax.swing— hierarchy of packages for platform-independent rich GUI components
java.applet— classes for creating an applet
The java.lang package is available without the use of an import statement.

Hmm, that's all right. But what happened to our classes which did not have any package declaration in them ?
Good question. In that scenario, all the classes are available under current default package.
Well, there are some more stuffs you need to be aware of,
  1. package should be the first line of any class
  2. Only one package statement is allowed in a class
  3. You can mention javac -d while compiling classes of any package
  4. To use the class, you must use the whole package name and classname

Hey, wait. I got the first two but what are the later two ???
Hmm, this needs a hands on. Otherwise, it is difficult to understand.

Let's write our greeting class once again.


/*
 * Copyright 2014-2015 Palash Kanti Kundu.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package greetings;

/**
 * @author Palash Kanti Kundu
 * @version 1.0
 * @since Sep 6, 2015
 */
public class Greetings {

 /**
  * @param args
  */
 public static void main(String[] args) {
  System.out.println("Hello World");
 }

}

The source code is written. Let's compile it.


javac Greetings.java

At this point, the source code is compiled and Greetings.class file is available in the directory.

Let's run it...



java Greetings

Hey, I am getting an error running this program. Everything worked fine. No issues in compilation. Then what ?
I suppose, you are getting error, similar to the following one,


Error: Could not find or load main class Greeting

So, what does this error tell you ?

It tells you that, there is no main method in the program. But you clearly remember that, there is indeed a main method and it should greet the world. Compilation was also successful.

So, what went wrong ?

Something obviously went wrong and that is known as package declaration. Remember, you put a package declaration in your class which means that Greetings class is available under greetings package.

So, to invoke Greetings class, you must mention the package.

Let's do some work and try some steps before running the program
  1. Create a directory named greetings
  2. move the Greetings.class file into the greetings directory
Following are the windows command to do this


mkdir greetings


move Greetings.class greetings/Greetings.class
Now, the generated class file is moved into the specified directory.

Now, let's run this using the package name,


java greetings.Greetings

Now you see the following output,


Hello World

Hmm, I understand this one. So, I have to compile the class, create the directory with the same name as that of the package and move the generated class file into the directory ad then run this using the <package name>.<class name>
Yes, that's perfect. But there is a shorthand to. Let's try that one too.

Its too much dude. Leave for today.
Just a few more steps and we'll be done. Please, carry on dear.

OK. Let's see.
First delete everything except the Greetings.java file.


cd greetings
del Greetings.class
cd ..
rmdir greetings

Now let's compile Greetings.java with the following command,

javac -d . Greetings.java

Now, check in the directory. You'll find a directory has been created with the name greetings automatically and Greetings.class into that directory.

Now, let's try to run the program using the earlier command,


java greetings.Greetings

And you'll find the following result,


Hello World

So, the compiler will create the directory itself for you. Now, that's it.

There is more about packages, we'll dive deep in the next section.

No comments:

Post a Comment