java coding adventure

Polish apples in Russia: what’s the package (in Java)?

What is a package?

The potato asked me yesterday: what actually is a package?

Let’s take a look at the project:

packages

In your *.java file, you also have the package blog.jumpstart.learningPath defined:

package blog.jumpstart.learningPath;

public class Potato {

	public static void main(String[] args) {
		Calculator calculator = new Calculator();
		System.out.println(calculator.add(1, 2));
	}

}

The package represents a real system folder. They are created in the source folders.

When you check the file properties (Right click on file Potato.java> Properties) you’ll see there the relative path to the file (inside your project file) and the file location (in other words absolute path):

fileProperties.PNG

If you click the button to the right of the location (the one circled with red) the exact folder where the file is stored will open.

What is the package for?

If you don’t want the people from Russia to eat your Polish apples you just keep them to themselves and don’t let Russians know that you have apples (the fact you have apples isn’t publicly known = is not public).

Packages protect your classes from the outside world. If your class shouldn’t be used in external classes (classes in other packages) just don’t make it public (more about public classes in Visit in the museum: access level – public (and what it means in Java)).

You also keep an order in your project thanks to the packages.

Import between packages

There is one more imporatant thing. Yesterday I asked you to create a Potato.java class in package blog.jumpstart.potato. Here’s the code of that package:

package blog.jumpstart.learningPath;\

import blog.jumpstart.learningPath.Calculator;

public class Potato {

	public static void main(String[] args) {
		Calculator calculator = new Calculator();
		System.out.println(calculator.add(1, 2));
	}

}

The line:

import blog.jumpstart.learningPath.Calculator;

means you are using something from differen package inside the class. Here, in class Potato you are using the class Calculator to create the object Calculator. But the class Claculator is in another package so you have to import it.

It’s as if you’d like to eat some Polish apples when you’re in Poland. There’s no problem: you go to the grocery and get Polish apples.

Then, you go to Russia and you still want to eat Polish apples. But before you can eat those apples they have to be imported to Russia.

When class is in the same package as another class you want to use you didn’t have to import import it. But when the class you want to use is in another package (or from the location standpoint in another folder) you have to import it.

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s