Java Visibility

From JavaProgramming.

Visibility of class members is almost the same as in C++, with a couple of important exceptions:

Default (no visibility specified)

Private Protected Public Here is a test program to demonstrate visibility:

 /* TestVisibility.java */
 class Classname {
   int defaultVar;
   public int publicVar;
   protected int protectedVar;
   private int privateVar;
 }

public class TestVisibility { public static void main(String[] args) { Classname foo = new Classname(); foo.defaultVar++; // Visible within the package foo.publicVar++; // Evidently public visibility foo.protectedVar++; // Visible within the package!!! foo.privateVar++; // Compiler error: privateVar has private access } }


EditText of this page (last edited April 24, 2002) or FindPage with title or text search