Ads Sponsored By Google :)

Tuesday 29 January 2013

how can we find|finding the modifiers of a class in java using reflection with example


finding the modifiers in java using reflection finding the modifiers in java with example how can we find the modifiers in java using reflection how can we find the modifiers in java  how can we find the modifiers in java using reflection with example finding the modifiers in java using reflection with example finding the modifiers of a class in java using reflection finding the modifiers of a class in java using reflection with example how can we find the modifiers of a class in java using reflection with example how can we find the modifiers of a class in java using reflection 

  Finding Modifiers of Class:

      Once a class is loaded into JVM we can get its modifiers by calling getModifiers(). When  
  we call getModifiers() it returns an encoded integer constructed by JVM for modifiers of class.  
  To decode encoded integer, we need to call methods of modifiers class. Modifier is a class of   
   java.lang.reflect package all declaring methods of modifier class are static methods. So we 
  can call these decoding methods directly with respect to class name. All decoding methods of 
  modifier class returns Boolean.

   For Example:
  
    //ReflectDemo6.java

      import java.lang.reflect.*;
          public class ReflectDemo6{
              public static void main(String args[]){
                        Class c=s.forName(args[0]);
                        int k=c.getModifiers();

                        if(Modifier.isPublic(k)) {
                          System.out.println(“It is public class”);
                          }

                     if(Modifier.isFinal(k)) {
                              System.out.prinltn(“It is Final class”);
                          }

                    if(Modifier.Abstract(k)) {
                            System.out.prinltn(“It is a Abstract class”);
                          }
  
                 if(Modifier.isProtected(k)) {
                         System.out.prinltn(“It is Protected class”);
                       } 
                  }



No comments:

Post a Comment

LinkWithin