How to set and get the field values using java Reflection | Reflection in java


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=hVvEcK5qi5M&list=UUhwKlOVR041tngjerWxVccw

Student.java
class Student
{
    public String name;
    public int age; 
}


ReflectionDemo.java
import java.lang.reflect.Field;

/**
 *
 * Getting and Setting Field Values.
 *
 */
public class ReflectionDemo
{
    public static void main(String[] args)
    {
        try
        {
            Class<Student> classObj = Student.class;
            /*
             * Returns:the Field object of this class specified by name
             */
            Field ageField = classObj.getField("age");
            System.out.println("Field Name = " + ageField.getName());
            System.out.println("Field Type = " + ageField.getType());

            Student student = classObj.newInstance();
            /*
             * Sets the field represented by this Field object on the specified
             * object argument to the specified new value.
             */
            ageField.set(student, 23);
            Object ageValue = ageField.get(student);
            System.out.println("age =  "+ageValue);
        }
        catch (NoSuchFieldException | SecurityException
                | IllegalArgumentException | IllegalAccessException
                | InstantiationException e)
        {
            e.printStackTrace();
        }
    }

}
Output
Field Name = age
Field Type = int
age =  23

Click the below link to download the code:
https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_get_set_field.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_get_set_field

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/eca56e82732b6bdecf5c1f7d98c23f5bc231a672/BasicJava/ReflectionDemo_get_set_field/?at=master

See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • How to instantiating the objects using constructor object - Java Reflection | Reflection in java



    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=ynUvXxzWWgM&list=UUhwKlOVR041tngjerWxVccw

    ReflectionDemo.java
    import java.lang.reflect.Constructor;
    import java.lang.reflect.InvocationTargetException;
    
    /**
     * 
     * Instantiating Objects using Constructor Object.
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
            try
            {
    
                Class classObj = String.class;
                /*
                 * Parameters:
                 * 
                 * parameterTypes - the parameter array
                 * 
                 * Returns: the Constructor object of the public
                 * constructor that matches the specified parameterTypes
                 * 
                 */
    
                Constructor constructor = classObj.getConstructor(StringBuffer.class);
                StringBuffer sb = new StringBuffer("Peter");
                /*
                 * Returns:a new object created by calling the constructor
                 * this object represents
                 */
                String str = (String) constructor.newInstance(sb);
                System.out.println(str);
            }
            catch (NoSuchMethodException | SecurityException
                    | InstantiationException | IllegalAccessException
                    | IllegalArgumentException
                    | InvocationTargetException e)
            {
                e.printStackTrace();
            }
    
        }
    
    }
    
    Output
    Peter
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_cons_instantiate.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_cons_instantiate

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/eca56e82732b6bdecf5c1f7d98c23f5bc231a672/BasicJava/ReflectionDemo_cons_instantiate/?at=master

    See also:

  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • How to access the constructor parameters of a class using Java Reflection | Reflection in java


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=bqzZ4jEFzWk&list=UUhwKlOVR041tngjerWxVccw

    ReflectionDemo.java
    import java.lang.reflect.Constructor;
    import java.lang.reflect.Parameter;
    import java.util.HashSet;
    
    /**
     * 
     * We can read what parameters a given constructor can takes. 
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
    
            Class<HashSet> classObj = HashSet.class;
    
            /*
             * Returns:the array of Constructor objects representing the public
             * constructors of this class
             */
    
            Constructor[] constructorArray= classObj.getConstructors();
    
            for (Constructor constructor : constructorArray)
            {
                System.out.println("constructor = "+constructor);
                System.out.println("Parameter Count = "+constructor.getParameterCount());
                /*
                 * Returns:an array of Parameter objects representing all the
                 * parameters to the executable this object represents.
                 */
                Parameter[] parameterArray = constructor.getParameters();
                for (Parameter parameter : parameterArray)
                {
                    System.out.println("Parameter Type = "+parameter.getParameterizedType());
                    System.out.println("Parameter Name = "+parameter.getName());
                }
                System.out.println("--------------------------------");
            }
    
        }
    
    }
    
    Output
    constructor = public java.util.HashSet(int)
    Parameter Count = 1
    Parameter Type = int
    Parameter Name = arg0
    --------------------------------
    constructor = public java.util.HashSet(int,float)
    Parameter Count = 2
    Parameter Type = int
    Parameter Name = arg0
    Parameter Type = float
    Parameter Name = arg1
    --------------------------------
    constructor = public java.util.HashSet(java.util.Collection)
    Parameter Count = 1
    Parameter Type = java.util.Collection<? extends E>
    Parameter Name = arg0
    --------------------------------
    constructor = public java.util.HashSet()
    Parameter Count = 0
    --------------------------------
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_cons_param.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_cons_param

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/eca56e82732b6bdecf5c1f7d98c23f5bc231a672/BasicJava/ReflectionDemo_cons_param/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • How to access the annotation of a class using Java Reflection | Reflection in java


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=5ipaLBAuM9s&list=UUhwKlOVR041tngjerWxVccw

    Display.java
    import java.lang.annotation.ElementType;
    import java.lang.annotation.Inherited;
    import java.lang.annotation.Retention;
    import java.lang.annotation.RetentionPolicy;
    import java.lang.annotation.Target;
    
    @Inherited
    @Target(ElementType.TYPE)
    @Retention(RetentionPolicy.RUNTIME)
    @interface MyAnnotation
        {
    
        }
    
    @MyAnnotation
    class Display
    {
        public void showMessage()
        {
            System.out.println("Hi");
        }
    }
    
    ReflectionDemo.java
    import java.lang.annotation.Annotation;
    
    /**
     * 
     * We can access the class annotations of a class.
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
    
            Class<Display> classObj = Display.class;
    
            /*
             * Returns:annotations present on this element.
             */
    
            Annotation[] annotationArray = classObj.getAnnotations();
    
            for (Annotation annotation : annotationArray)
            {
                System.out.println(annotation);
            }
    
        }
    
    }
    
    Output
    @MyAnnotation()
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_access_annotation.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_access_annotation

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/eca56e82732b6bdecf5c1f7d98c23f5bc231a672/BasicJava/ReflectionDemo_access_annotation/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Friday, 27 October 2017

    How to access the fields of a class using Java Reflection | Reflection in java


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=cYUWoR8X5IM&list=UUhwKlOVR041tngjerWxVccw

    Student.java
    class Student
    {
        public String name;
        public String age;
        
        public void showMessage()
        {
            System.out.println("Hi");
        }
    }
    
    ReflectionDemo.java
    import java.lang.reflect.Field;
    
    /**
     *
     * We can access the fields (member variables) of a class.
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
    
            Class<Student> classObj = Student.class;
    
            /*
             * Returns:the array of Field objects representing the public fields.
             */
    
            Field[] fieldArray = classObj.getFields();
    
            for (Field field : fieldArray)
            {
                System.out.println(field);
                System.out.println(field.getName());
                System.out.println("---------------------------------");
            }
    
        }
    
    }
    
    Output
    public java.lang.String Student.name
    name
    ---------------------------------
    public java.lang.String Student.age
    age
    ---------------------------------
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_access_fields.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_access_fields

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_access_fields/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Thursday, 26 October 2017

    How to access the methods of a class using Java Reflection | Reflection in java


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=yVR-4jTp2fM&list=UUhwKlOVR041tngjerWxVccw

    ReflectionDemo.java
    import java.lang.reflect.Method;
    import java.util.ArrayList;
    
    /**
     * 
     * We can access the methods of a class.
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
    
            Class<ArrayList> classObj = ArrayList.class;
    
            /*
             * Returns:the array of Method objects representing the public methods
             * of this class.
             */
    
            Method[] methodArray = classObj.getMethods();
    
            for (Method method : methodArray)
            {
                System.out.println(method);
            }
    
        }
    
    }
    
    Output
    public boolean java.util.ArrayList.add(java.lang.Object)
    public void java.util.ArrayList.add(int,java.lang.Object)
    public boolean java.util.ArrayList.remove(java.lang.Object)
    public java.lang.Object java.util.ArrayList.remove(int)
    public java.lang.Object java.util.ArrayList.get(int)
    public java.lang.Object java.util.ArrayList.clone()
    public int java.util.ArrayList.indexOf(java.lang.Object)
    public void java.util.ArrayList.clear()
    public boolean java.util.ArrayList.contains(java.lang.Object)
    public boolean java.util.ArrayList.isEmpty()
    public java.util.Iterator java.util.ArrayList.iterator()
    public int java.util.ArrayList.lastIndexOf(java.lang.Object)
    public void java.util.ArrayList.replaceAll(java.util.function.UnaryOperator)
    public int java.util.ArrayList.size()
    public java.util.List java.util.ArrayList.subList(int,int)
    public java.lang.Object[] java.util.ArrayList.toArray(java.lang.Object[])
    public java.lang.Object[] java.util.ArrayList.toArray()
    public java.util.Spliterator java.util.ArrayList.spliterator()
    public boolean java.util.ArrayList.addAll(int,java.util.Collection)
    public boolean java.util.ArrayList.addAll(java.util.Collection)
    public void java.util.ArrayList.forEach(java.util.function.Consumer)
    public java.lang.Object java.util.ArrayList.set(int,java.lang.Object)
    public void java.util.ArrayList.ensureCapacity(int)
    public void java.util.ArrayList.trimToSize()
    public java.util.ListIterator java.util.ArrayList.listIterator()
    public java.util.ListIterator java.util.ArrayList.listIterator(int)
    public boolean java.util.ArrayList.removeAll(java.util.Collection)
    public boolean java.util.ArrayList.removeIf(java.util.function.Predicate)
    public boolean java.util.ArrayList.retainAll(java.util.Collection)
    public void java.util.ArrayList.sort(java.util.Comparator)
    public boolean java.util.AbstractList.equals(java.lang.Object)
    public int java.util.AbstractList.hashCode()
    public java.lang.String java.util.AbstractCollection.toString()
    public boolean java.util.AbstractCollection.containsAll(java.util.Collection)
    public final void java.lang.Object.wait() throws java.lang.InterruptedException
    public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException
    public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException
    public final native java.lang.Class java.lang.Object.getClass()
    public final native void java.lang.Object.notify()
    public final native void java.lang.Object.notifyAll()
    public default java.util.stream.Stream java.util.Collection.stream()
    public default java.util.stream.Stream java.util.Collection.parallelStream()
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_access_methods.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_access_methods

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_access_methods/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • How to access the constructors of a class using Java Reflection | Reflection in java


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=-1k7JmxjPvQ&list=UUhwKlOVR041tngjerWxVccw

    ReflectionDemo.java
    import java.lang.reflect.Constructor;
    import java.util.ArrayList;
    
    /**
     * 
     * We can access the constructors of a class.
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
    
            Class<ArrayList> classObj = ArrayList.class;
    
            /*
             * Returns:the array of Constructor objects representing the public
             * constructors of this class
             */
    
            Constructor<?>[] constructorArray = classObj.getConstructors();
    
            for (Constructor constructor : constructorArray)
            {
                System.out.println(constructor);
                System.out.println("Name = "+constructor.getName());
                System.out.println("ParameterCount = "+constructor.getParameterCount());
                System.out.println("-----------------------------");
            }
    
        }
    
    }
    
    Output
    public java.util.ArrayList(java.util.Collection)
    Name = java.util.ArrayList
    ParameterCount = 1
    -----------------------------
    public java.util.ArrayList()
    Name = java.util.ArrayList
    ParameterCount = 0
    -----------------------------
    public java.util.ArrayList(int)
    Name = java.util.ArrayList
    ParameterCount = 1
    -----------------------------
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_access_constructors.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_access_constructors

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_access_constructors/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • How to get the list of interfaces implemented by a given class using Java Reflection


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=if20F7TutsM&list=UUhwKlOVR041tngjerWxVccw

    ReflectionDemo.java
    import java.util.ArrayList;
    
    /**
     * 
     * It is possible to get a list of the interfaces implemented by a given class
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
    
            Class<ArrayList> classObj = ArrayList.class;
    
            /*
             * Returns:an array of interfaces implemented by this class.
             * 
             * A class can implement many interfaces. Therefore an array of Class is
             * returned. Interfaces are also represented by Class objects in Java
             * Reflection.
             */
    
            Class[] classArray = classObj.getInterfaces();
    
            for (Class class1 : classArray)
            {
                System.out.println(class1.getName());
            }
    
        }
    
    }
    
    Output
    java.util.List
    java.util.RandomAccess
    java.lang.Cloneable
    java.io.Serializable
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_access_interfaces.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_access_interfaces

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_access_interfaces/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • How to access the superclass of the class using Java Reflection | Reflection in java


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=ENu41BqBqZU&list=UUhwKlOVR041tngjerWxVccw

    ReflectionDemo.java
    import java.util.ArrayList;
    
    /**
     * 
     * From the Class object we can access the superclass of the class.
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
    
            Class<ArrayList> classObj = ArrayList.class;
    
            /*
             * Returns:the superclass of the class represented by this object.
             */
    
            Class<? super ArrayList> superClass = classObj.getSuperclass();
    
            System.out.println(superClass);
            System.out.println("Name = "+superClass.getName());
            System.out.println("Simple Name = "+superClass.getSimpleName());
    
        }
    
    }
    
    Output
    class java.util.AbstractList
    Name = java.util.AbstractList
    Simple Name = AbstractList
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_access_superclass.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_access_superclass

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_access_superclass/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • How to get the package information of the class using Java Reflection | Reflection in java


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=qmFeRSOSJFU&list=UUhwKlOVR041tngjerWxVccw

    ReflectionDemo.java
    import java.util.ArrayList;
    
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
    
            Class<ArrayList> classObj = ArrayList.class;
            /*
             * Gets the package for this class. The class loader of this class is
             * used to find the package.
             */
            Package packageObj = classObj.getPackage();
            /*
             * Returns:The fully-qualified name of this package
             */
            System.out.println("Package Name = " + packageObj.getName());
            /*
             * Returns:the title of the implementation, null is returned if it is not known.
             */
            System.out.println("ImplementationTitle = "+packageObj.getImplementationTitle());
            /*
             * Returns:the version of the implementation, null is returned if it is not known.
             */
            System.out.println("ImplementationVersion = "+packageObj.getImplementationVersion());
            /*
             * Returns:the specification vendor, null is returned if it is not known.
             */
            System.out.println("SpecificationVendor = "+packageObj.getSpecificationVendor());
            
            /*
             * Returns:the specification title, null is returned if it is not known.
             */
            System.out.println("SpecificationTitle = "+packageObj.getSpecificationTitle());
        }
    
    }
    
    Output
    Package Name = java.util
    ImplementationTitle = Java Runtime Environment
    ImplementationVersion = 1.8.0_144
    SpecificationVendor = Oracle Corporation
    SpecificationTitle = Java Platform API Specification
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_package_info.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_package_info

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_package_info/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • How to call the parameterized private method using Java Reflection | Reflection in java


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=P4rEHouJ4YU&list=UUhwKlOVR041tngjerWxVccw

    Display.java
    public class Display
    {
        private void DisplayNumber(int i)
        {
            System.out.println(i);
        }
    }
    
    ReflectionDemo.java
    import java.lang.reflect.Method;
    
    /**
     * How to call parameterized private method from another class?
     * 
     * We can call the private method from outside the class by changing the runtime
     * behaviour of the class.
     * 
     * By the help of "java.lang.Class" class and "java.lang.reflect.Method" class,
     * we can call private method from any other class.
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
            try
            {
                Class<Display> classObj = Display.class;
                Object displayObject = classObj.newInstance();
    
                Method method = classObj.getDeclaredMethod("DisplayNumber", new Class[] { int.class });
                method.setAccessible(true);
                method.invoke(displayObject, 1000);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
        }
    
    }
    
    Output
    1000
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_param_private_method.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_param_private_method

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_param_private_method/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • How to call the private method using Java Reflection | Reflection in java


    Student.java
    public class Student
    {
        private String firstName = "Peter";
        private String lastName = "J";
    
        
        private void showStudentFullName()
        {
            System.out.println(firstName + " " + lastName);
        }
    
    }
    
    ReflectionDemo.java
    import java.lang.reflect.Method;
    
    /**
     * How to call private method from another class in java?
     * 
     * We can call the private method from outside the class by changing
     * the runtime behaviour of the class.
     * 
     * By the help of "java.lang.Class" class and
     * "java.lang.reflect.Method" class, we can call private method from
     * any other class.
     *
     */
    public class ReflectionDemo
    {
        public static void main(String[] args)
        {
            try
            {
                Class<?> classObj = Class.forName("Student");
                Student studentObj = (Student) classObj.newInstance();
                Method method = classObj.getDeclaredMethod("showStudentFullName", null);
                method.setAccessible(true);
                /*
                 * Parameters:
                 * 
                 * obj - the object the underlying method is invoked from
                 * 
                 * args - the arguments used for the method call
                 * 
                 * Returns: the result of dispatching the method
                 * represented by this object on obj with parameters args
                 * 
                 */
    
                method.invoke(studentObj, null);
            }
            catch (Exception e)
            {
                e.printStackTrace();
            }
    
        }
    
    }
    
    Output
    Peter J
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/ReflectionDemo_Call_Private_method.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/ReflectionDemo_Call_Private_method

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/6c95796df09abfb09587d07613e2e6d5f9ad024e/BasicJava/ReflectionDemo_Call_Private_method/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial