Title: More Classes 4
Course: CPS 161: An Intro to Programming with Java - DL MM MASTER

More Classes 4 : Packages, Access Levels

Basic rules:

Every class lives in a Package which is very much like a folder. If you don't explicitly specify a package then you are likely in the default package. In general, Java discourages the default package. I have used the default package almost exclusively because I was trying to keep things simple so far.

So we will create a package called my_package and place the following code in it. Place the following file in it:

my_package.Module.java


package my_package;
// The package statement above must be the first non-comment statement in
// your file

public class Module {

    public void public_method()
    {
        System.out.println("public_method");
    }
    void friendly_method()
    {
        System.out.println("friendly method");
    }
    private void private_method()
    {
        System.out.println("private method");
    }

    public static void main(String[] args)
    {
        Module m = new Module();
        m.private_method();
        m.friendly_method();
        m.public_method();


        Module2 m2 = new Module2();
        //m2.private_method();
        m2.friendly_method();
        m2.public_method();

    }
}

//*****************************************************
//******    Another class  ****************************
//*****************************************************

class Module2  // Note this class is not declared public
{
    public void public_method()
    {
        System.out.println("public_method");
    }
    void friendly_method()
    {
        System.out.println("friendly method");
    }
    private void private_method()
    {
        System.out.println("private method");
    }

}



Every instance variable and method will have an access level:

- public
- private
- friendly, default

If you declare your class to be public, then the name of your file must match the name of your class. Note that this means there can be at most one public class in a java source file.

If you leave off public on the class, then you can have more than one class in a file.

my_package.Test.java


package my_package;

public class Test {

    public static void main(String[] args)
    {
        Module m = new Module();
        //m.private_method();
        m.friendly_method();  // Acts like public in it's own package
        m.public_method();




        Module2 m2 = new Module2();
        //m2.private_method();
        m2.friendly_method(); // Acts like public in it's own package
        m2.public_method();

    }

}



See what happens when you put Test.java into another package. For example:

some_users.Test.java

- Note that you will be forced to have the import:
import my_package.Module;
- You will lose access to everything except for:
Module.public_method()
- The friendly_method acts like a private method in another package.
- Any class that doesn't have public on it (like Module2), is not accessable at all in another package.

Practical Usage:

You are tired of the superfluous typing in System.out...... or constructing the Scanner all of the time. Then the following package may be for you.

mytools.P.java


package mytools;

public class P {

    public static void println(String s)
    {
        System.out.println(s);
    }
    public static void print(String s)
    {
        System.out.println(s);
    }

}


mytools.In.java

Don't worry about the exception catching below ... this is a topic that will be covered at the end of the course


package mytools;

import java.util.InputMismatchException;
import java.util.Scanner;

public class In {
    static Scanner keyboard = new Scanner(System.in);

    public static String next()
    {
        return keyboard.next();
    }
    public static String nextLine()
    {
        return keyboard.nextLine();
    }
    public static int nextInt()
    {
        try
        {
            return keyboard.nextInt();
        }
        catch (InputMismatchException e)
        {
            System.out.println("nextInt oops");
            return -1;
        }
    }
    public static double nextDouble()
    {
        try
        {
            return keyboard.nextDouble();
        }
        catch (InputMismatchException e)
        {
            System.out.println("nextDouble oops");
            return -1.0;
        }
    }

}



some_users.LazyIO.java


package some_users;

import mytools.In;
import mytools.P;

public class LazyIO {

	public static void main(String[] args) {
		P.println("Welcome to my Lazy I/O program");
		P.println("Enter in your name");
		String name = In.next();
		
		P.println("Enter in your age");
		int age = In.nextInt();
		
		P.println("Your age is " + age);

	}

}



Last Modified: September 14, 2013 3:17 PM