How do I get modifiers of a class object?

Category: java.lang.reflect, viewed: 2727 time(s).
package org.kodejava.example.reflect;

import java.lang.reflect.Modifier;

public class ClassModifier {
    public static void main(String[] args) {
        getClassModifier(String.class);        
        getClassModifier(TestA.class);
        getClassModifier(TestB.class);
    }

    private static void getClassModifier(Class clazz) {
        int modifier = clazz.getModifiers();

        //
        // Return true if the integer argument includes the public modifier,
        // false otherwise.
        //
        if (Modifier.isPublic(modifier)) {
            System.out.println(clazz.getName() + " class modifier is public");
        }

        //
        // Return true if the integer argument includes the protected modifier,
        // false otherwise.
        //
        if (Modifier.isProtected(modifier)) {
            System.out.println(clazz.getName() + " class modifier is protected");
        }

        //
        // Return true if the integer argument includes the private modifier,
        // false otherwise.
        //
        if (Modifier.isPrivate(modifier)) {
            System.out.println(clazz.getName() + " class modifier is private");
        }

        //
        // Return true if the integer argument includes the static modifier,
        // false otherwise.
        //
        if (Modifier.isStatic(modifier)) {
            System.out.println(clazz.getName() + " class modifier is static");
        }

        //
        // Return true if the integer argument includes the final modifier,
        // false otherwise.
        //
        if (Modifier.isFinal(modifier)) {
            System.out.println(clazz.getName() + " class modifier is final");
        }

        //
        // Return true if the integer argument includes the abstract modifier,
        // false otherwise.
        //
        if (Modifier.isAbstract(modifier)) {
            System.out.println(clazz.getName() + " class modifier is abstract");
        }
    }

    protected static final class TestA {
    }

    private abstract class TestB {
    }
}
Click here to lend your support to: Kode Java Org and make a donation at www.pledgie.com !

 

Uncensored Newsgroups
Download Hundreds of Complimentary Industry Resources

Get hundreds of popular Industry magazines, white papers, webinars, podcasts, and more; all available at no cost to you. With more than 600 complimentary offers, you'll find plenty of titles to suit your professional interests and needs. Click Here and Sign up today!

Java Training

Sponsored Links

Our Friends

Statistics

Locations of visitors to this page
visitor stats