public

Method syntax

>>-public-+----------+-+--------+-+-------+-+--------------+->
          '-abstract-' '-static-' '-final-' '-synchronized-'

>-+--------+-ResultType-Identifier-(-+---------------------+-)->
  '-native-'                         '-FormalParameterList-'

>-+----------------------+-MethodBody-><
  '-throws-ClassTypeList-'

Field syntax

>>-public-+----------+-+--------+-+-----------+-Type->
          +-final----+ '-static-' '-transient-'
          '-volatile-'

>-+-Identifier--------------+-+------------------------+-;-><
  |                 v-----' | '-=-+-Expression-------+-'
  '-ArrayIdentifier-+-[-]-+-'     '-ArrayInitializer-'

Description
If a class or interface type is declared public, then it may be accessed by any Java code that can access the package in which it is declared. If a member or constructor is declared public, then access to it is permitted. All members of interfaces are implicitly public.

A public class member or constructor is accessible throughout the package where it is declared and from any other package that has access to the package in which it is declared. Public class members are inherited by subclasses and subinterfaces.

Example
In the following compilation unit, the public class Point has as public members: the move method and the moves field.

package points;

public class Point {
        int x, y;

        public void move(int dx, int dy) {
                x += dx; y += dy;
                moves++;
        }
        public static int moves = 0;
}

These public members are accessible to any other package that has access to package points. The fields x and y are not public and therefore are accessible only from within the package points.

ngrelr.gif (548 bytes)
Syntax diagrams
Access control
abstract keyword
class keyword
final keyword
interface keyword
native keyword
static keyword
synchronized keyword
throws keyword
transient keyword

Source: The Java Language Specification. Copyright (C) 1996 Sun Microsystems, Inc.