Change Final variable value in Java

Have you ever thought of changing final variable in Java !!.

We all know that once the final variable is assigned value we can’t change it. When ever we try to change the value we get compilation error. But there is an option to change the value at runtime through Reflection API.

Reflection is the process of examining or modifying the runtime behavior of a class at runtime though we can’t change the structure of the class. The java.lang.Class class provides many methods that can be used to get metadata, examine and change the runtime behaviour of a class. We can do any thing related to object like changing variables, invoking methods, calling constructors, changing accessibility of the variables, methods etc…

So let’s see how we change final value

public class ReflectionExample {
  public static void main(String args[]) throws Exception{
	  ChangeFinalVariable cv = new ChangeFinalVariable();
	  System.out.println("Before: "+cv.getMessage());

	  //Get declared field from class
	  Field f = cv.getClass().getDeclaredField("message");

	  // set the accessiblity of the field to true, this will enable you to change the value
	  f.setAccessible(true); 

	  //change the field value
	  f.set(cv, "No that's not true you are changed now!!!");

	  System.out.println("After: "+cv.getMessage());
  }
}

class ChangeFinalVariable{
	public final String message;

	public ChangeFinalVariable() {
		this.message = "You Can't Change Me";
	}

	public String getMessage() {
		return message;
	}
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s