Home » Java programming language

Java Class class getModifiers() method with example

Class class getModifiers() method: Here, we are going to learn about the getModifiers() method of Class class with its syntax and example.
Submitted by Preeti Jain, on November 14, 2019

Class class getModifiers() method

  • getModifiers() method is available in java.lang package.
  • getModifiers() method is used to return the modifiers for this class or interfaces and the modifiers consist of JVM constants for the modifiers public, private, protected, final, static, abstract and interface and these modifiers should be decoded by the method getModifier() of Modifier class.
  • getModifiers() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
  • getModifiers() method does not throw an exception at the time of encoding a modifier.

Syntax:

    public int getModifiers();

Parameter(s):

  • It does not accept any parameter.

Return value:

The return type of this method is int, it returns an integer value representing the modifiers for the class or an interfaces.

Example:

// Java program to demonstrate the example 
// of int getModifiers () method of Class 

import java.lang.reflect.*;

public class GetModifiersOfClass {
    public static void main(String[] args) {
        Thread th = new Thread();

        // Get Class object of Thread
        Class cl = th.getClass();

        // It return the modifier of the class Thread
        int modifiers = cl.getModifiers();

        // Display Modifier in integer form 
        System.out.println("Before Conversion = " + modifiers);

        // Convert Modifer from int to String by using toString()
        String modifier = Modifier.toString(modifiers);

        // Display Modifier in String form 
        System.out.print("After Conversion = " + modifier);
    }
}

Output

Before Conversion = 1
After Conversion = public



Comments and Discussions!

Load comments ↻






Copyright © 2024 www.includehelp.com. All rights reserved.