Packages Part 1 Tutorial

Java provides us with the ability to secure the members of a package using access modifiers. There are a total of four types of access modifiers: public, private, protected, and default. Access modifiers will have numerous tutorials dedicated to each of the types. This tutorial will demonstrate the use of the public access modifier across different packages. The Packages tutorial mini-series will use packages and classes from previous package tutorials. You will need to keep those packages and classes around to complete the examples in this mini-series. In this tutorial I will create a location package with five classes: Nation, State, County, City, and MyLocation. Then I will modify the Hello class in the aboutme package to call a method from the MyLocation class in the location package.

Open the command prompt (CMD - see the Getting Started ) and type in the following commands.

C:\Windows\System32>cd \
C:\>md Java
C:\>cd Java
C:\Java>
C:\Java>md location
C:\Java>cd location
C:\Java\location>Notepad Nation.java

Copy and Paste, or type the following code into Notepad and be sure to save the file when you are done.


package location; 

class Nation {
   String nationName = "United States";
}

Now switch back to the command prompt (CMD) and type in Notepad State.java and press Enter.


package location; 

class State extends Nation {
    String stateName = "Florida";
}

Now switch back to the command prompt (CMD) and type in Notepad County.java and press Enter.


package location; 

class County extends State {
    String countyName = "Orange County";
}

Now switch back to the command prompt (CMD) and type in Notepad City.java and press Enter.


package location; 

class City extends County {
    String cityName = "Orlando";
}

Now switch back to the command prompt (CMD) and type in Notepad MyLocation.java and press Enter.


package location; 
public class MyLocation extends City {
    public String whereILive() {
        return (cityName + ", " + countyName +", " + stateName + ", " + nationName);
    }
}

Now switch back to the command prompt (CMD) and type in cd .. and press Enter.
Now type in javac location\*.java to compile all the source code files in the location folder and press Enter.
That command will compile all the source code files in the location folder.


C:\Java\location>cd ..
C:\Java\javac location\*.java

Now type in cd aboutme and press Enter.
We are going to modify our Hello.java file from the last tutorial. Now type in Notepad Hello.java and press Enter.


package aboutme;

class Hello {
    public static void main(String args[]) {
        String name = "Dan";
        System.out.println("Hello everybody, my name is " + name);

        location.MyLocation reside = new location.MyLocation();	// new
        System.out.println("I live in " + reside.whereILive()); // new
    }
}

Now switch back to the command prompt (CMD) and type in cd .. and press Enter.
Now switch back to the command prompt (CMD) and type in javac aboutme\Hello.java and press Enter.
Now type in java aboutme.Hello and press Enter.


C:\Java\aboutme>cd ..
C:\Java\javac aboutme\Hello.java
C:\Java\javac aboutme.Hello
Hello everybody, my name is Dan
I live in Orlando, Orange County, Florida, United States


Final thoughts

The public access modifier applied to classes and methods allows full access to members of other packages. There was a lot to take in on this tutorial; with practice everything will make sense.


Tutorials