A class declaration may include the following modifiers:
public
, abstract
, or final
. The class modifiers precede the class
keyword in the class definition. In the following example, the class modifiers are public
and final
: At run time, to identify the modifiers of a class you perform these steps:public final Coordinate {int x, int y, int z}
- Invoke
getModifiers
upon aClass
object to retrieve a set of modifiers. - Check the modifiers by calling
isPublic
,isAbstract
, andisFinal
.
String
class. The output of the sample program reveals that the modifiers of theimport java.lang.reflect.*;
import java.awt.*;
class SampleModifier {
public static void main(String[] args) {
String s = new String();
printModifiers(s);
}
public static void printModifiers(Object o) {
Class c = o.getClass();
int m = c.getModifiers();
if (Modifier.isPublic(m))
System.out.println("public");
if (Modifier.isAbstract(m))
System.out.println("abstract");
if (Modifier.isFinal(m))
System.out.println("final");
}
}
String
class are public
and final
: public
final
No comments:
Post a Comment