Java Reference
In-Depth Information
The following is a sample class that declares many class members with different access levels:
// AccessLevelSample.java
package com.jdojo.cls;
// Class AccessLevelSample has public access level
public class AccessLevelSample {
private int num1; // private access level
int num2; // package-level access
protected int num3; // protected access level
public int num4; // public access level
public static int count = 1; // public access level
// m1() method has private access level
private void m1() {
// Code goes here
}
// m2() method has package-level access
void m2() {
// Code goes here
}
// m3() method has protected access level
protected void m3() {
// Code goes here
}
// m4() method has public access level
public void m4() {
// Code goes here
}
// doSomething() method has private access level
private static void doSometing() {
// Code goes here
}
}
Note that access levels can be specified for both instance and static members of a class. It is a convention to
specify the access level modifier as the first modifier in the declaration. If you declare a static field for a class that is
public, you should use the public modifier first, and then the static modifier, as a convention. For example, both of
the following declarations for an instance variable num , are valid:
// Declaration #1
public static int num; // Conventionally used
// Declaration #2
static public int num; // Technically correct, but Conventionally not used.
 
Search WWH ::




Custom Search