Classes, Access, and Multipart Programs in Java

By Barry Burd

With this topic, you can become all tangled up in terminology, so you need to get some basics out of the way. Here’s a fake piece of Java code as example:

class MyClass {
    int myField;              //a field
                              // (a member)
    void myMethod() {         //a method (another member)
        int myOtherField;     //a method-local variable
                              // (NOT a member)
    }
}

The comments on the right side of the code tell the whole story. Two kinds of variables exist here — fields and method-local variables. This example isn’t about method-local variables. It’s about methods and fields.

Carrying around the phrase “methods and fields” wherever you go isn’t easy. It’s much better to give these things one name and be done with it. That’s why both methods and fields are called members of a class.

Members versus classes

At this point, you make an important distinction. Think about Java’s public keyword. As you may already know, you can put public in front of a member. For example, you can write

public static void main(String args[]) {

or

public amountInAccount = 50.22;

These uses of the public keyword come as no big surprise. What you may not already know is that you can put the public keyword in front of a class. For example, you can write

public class Drawing {
    // Your code goes here
}

In Java, the public keyword has two slightly different meanings — one meaning for members and another meaning for classes.

Access modifiers for members

Sure, this section is about members. But that doesn’t mean that you can ignore Java classes. Members or not, the Java class is still where all the action takes place. Each field is declared in a particular class, belongs to that class, and is a member of that class.

The same is true of methods. Each method is declared in a particular class, belongs to that class, and is a member of that class. Can you use a certain member name in a particular place in your code? To begin answering the question, check whether that place is inside or outside of the member’s class:

If the member is private, only code that’s inside the member’s class can refer directly to that member’s name.

class SomeClass {
    private int myField = 10;
}
class SomeOtherClass {
    public static void main(String args[]) {
        SomeClass someObject = new SomeClass();
        //This doesn't work:
        System.out.println(someObject.myField);
    }
}

If the member is public, any code can refer directly to that member’s name.

class SomeClass {
    public int myField = 10;
}
class SomeOtherClass {
    public static void main(String args[]) {
        SomeClass someObject = new SomeClass();
        //This works:
        System.out.println(someObject.myField);
    }
}

These figures illustrate the ideas in a slightly different way.

Several classes and their subclasses.

Several classes and their subclasses.
The range of code in which a public field or method can be used (shaded).

The range of code in which a public field or method can be used (shaded).
The range of code in which a private field or method can be used (shaded).

The range of code in which a private field or method can be used (shaded).