Discovering Class Modifiers


Class c = someObject.getClass(); int mods = c.getModifiers(); if (Modifier.isPublic(mods))    System.out.println("public"); if (Modifier.isAbstract(mods))    System.out.println("abstract"); if (Modifier.isFinal(mods))    System.out.println("final");



In a class definition, keywords called modifiers can precede the class keyword. The modifiers available are: public, abstract, and final. To discover which modifiers have been applied to a given class, you first get a Class object representing that class using the getClass() method. Next, you would call the getModifiers() method on the class object to return a bitmapped int value representing the modifiers. You can then use static methods of the java.lang.reflect.Modifier class to determine which modifiers have been applied. The static methods available are: isPublic(), isAbstract(), and isFinal().

Note

If you have a class object that may represent an interface, you might also want to use the isInterface() method. This method will return true if the modifiers passed in include the interface modifier.

The Modifier class also contains additional static methods for determining which modifiers have been applied to class methods and variables. These methods include: isPrivate(), isProtected(), isStatic(), isSynchronized(), isVolatile(), isTransient(), isNative(), and isStrict().





JavaT Phrasebook. Essential Code and Commands
Java Phrasebook
ISBN: 0672329077
EAN: 2147483647
Year: 2004
Pages: 166

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net