Java: Access to classes and members

By default, top-level classes are accessible within the package in which they are defined. However, if a top-level class is declared public, it is accessible everywhere.

The members of a class are always accessible within the body of the class. By default, members are also accessible throughout the package in which the class is defined.

This default level of access is often called package access. It is only one of four possible levels of access. The other three levels are defined by the public, protected, and private modifiers.

package javaapplication40;

public class Music {

    private String artiste;
    protected String rockband;

    public void live() {
        this.artiste = "Lady Gaga";

    public void record() {
        this.rockband = "Nirvana";

    public static void main(String[] args) {

        Music m = new Music();;



Series Navigation<< Java: Overriding superclass method
Java: Rules for accessing members >>