Sunday, February 16, 2014
Today I will show that how the power of reflection can bypass encapsulation. This is excellent feature in Java. You must have read and experienced that you cannot create instances of class with private constructors using "new" keyword outside that class. But using reflection you can do mit very easily.And using that we can call any methods and do as we wish. To do that we have to follow these steps :
- Create the Class class instance of the specified class using Class.forName() or <Class-Name>.class
- Create the Constructor instance from the Class instance using getDeclaredConstructor()
- Call the setAccessible() on constructor object and set to true. This is the most important step as failing it wont allow you to access the private constructor.
- Call newInstance() method on the constructor object which will return a reference to Object class. That's it. Dow what you wish with this reference.
-------------------------------------------------------------------------------------------------------------------------
Java Source Code
-------------------------------------------------------------------------------------------------------------------------
import java.lang.reflect.Constructor; class PrivateCons { private String i; private PrivateCons(String i){ this.i=i; } @Override public String toString(){ return "I am "+i; } } public class PrivateConsReflect{ public static void main(String[] args) throws Exception{ Class c=PrivateCons.class; //getting Class class instance Constructor cons=c.getDeclaredConstructor(String.class); //getting constructor cons.setAccessible(true); //setting true to access private feature Object pc=cons.newInstance("5"); //creating instance of the class System.out.println(pc); //printing object } }
-------------------------------------------------------------------------------------------------------------------------
Download Links
-------------------------------------------------------------------------------------------------------------------------
Labels:Reflection | 0
comments
Subscribe to:
Posts
(Atom)
Total Pageviews
Followers
Labels
- Algorithms (7)
- Annotation (3)
- Files (6)
- Generics (3)
- Graphics2D (5)
- Graphics2D-Images (7)
- Inheritance (2)
- J2EE (9)
- Java 8 (4)
- Java FAQs (19)
- JDBC (3)
- Networking (2)
- Packages (1)
- Reflection (4)
- Security (7)
- Sorting (2)
- Swing (3)
- Threads (3)
- Utils (3)
Popular Posts
-
Today I will show you how you can implement Bankers algorithm in Java. The Banker's algorithm is a resource allocation and deadlock a...
-
------------------------- UPDATE ------------------------- I have updated the code on request of some followers so that they can directly...
-
Today I am going to show how to convert a postfix expression to an infix expression using stack in Java. In an earlier post here we ...
-
Today in this article I will tell you how to convert an infix expression to postfix expression using stack. This is an important applicat...
-
--------------------UPDATE------------------- I have updated my post so that now it can detect IE 11. This modification was necessary as t...
-
Today I am going to show you how you can generate and validate captcha. A CAPTCHA (an acronym for "Completely Automated Public Turin...
-
Today I am going to post a program that will be able to produce all the mColorings of a given graph G. What is mColoring : The problem st...
-
Today in this article I will show you how to create or develop a Tower of Hanoi game in Java. The Tower of Hanoi is a famous problem tha...