The above code is working fine, but when I tried the same thing with provided class like java.lang.String and tried access the private member of it, it threw an exception for access. Though the access can be restricted by setting SecurityManager which I tried in my case also and worked , but in java.lang.String I was unable to find any implementation for the SecurityManager which disallowed the access to private members, then how is it that I am not able to access it using reflection?

Also in the same above example,when I made by _count variable as final, and tried changing the value using reflection it did not threw any exception at runtime but did not change value either.

If you google for "changing string via reflection java", the first few results show that this should work. There is also some code there that you can try and compare with yours. If what you experience is indeed so, it must be something new in Java. Perhaps for plugging some security hole.

baftos wrote:
If you google for "changing string via reflection java", the first few results show that this should work. There is also some code there that you can try and compare with yours. If what you experience is indeed so, it must be something new in Java. Perhaps for plugging some security hole.

that wouldn't make any sense. if you have no SecurityManager running, you have no security in java, period. so throwing a SecurityException with no SecurityManager would be pointless (there are a million other ways you could "exploit" java if you don't have a SecurityManager, e.g. "System.exec("format c:")").

baftos wrote:
If you google for "changing string via reflection java", the first few results show that this should work. There is also some code there that you can try and compare with yours. If what you experience is indeed so, it must be something new in Java. Perhaps for plugging some security hole.

that wouldn't make any sense. if you have no SecurityManager running, you have no security in java, period. so throwing a SecurityException with no SecurityManager would be pointless (there are a million other ways you could "exploit" java if you don't have a SecurityManager, e.g. "System.exec("format c:")").

Booooo, unfair example. If you can execute format c: through a piece of Java code, you fail at setting up proper user rights. It has nothing to do with security in Java.

Exception in thread "main" java.lang.IllegalArgumentException: Can not set final [C field java.lang.String.value to java.lang.String
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeFieldAccessorImpl.throwSetIllegalArgumentException(Unknown Source)
at sun.reflect.UnsafeQualifiedObjectFieldAccessorImpl.set(Unknown Source)
at java.lang.reflect.Field.set(Unknown Source)
at Y.main(Y.java:8)

Yeah agree with you on it would let modify the value of char[] variable.
But as I found in String.java, value is of type char[] and is declared as final. The question is how come it let me modify the final declared variable of type array. My learning so far from some reading is that , arrays can not be final and only the handle to the array can be only so.
So is it correct ? If so , appreciate help on understanding.

If Type is an array or not, is irrelevant. The second line should not compile because var (the variable!) is final. The keyword final refers to the variable, not to the value.
You misread 'arrays cannot be final'. I think you read something to the effect that even if the variable referencing an array is final, the contents of the array can still be changed.
This is true for any (mutable) object.
Our exercise only demonstrated that using reflection you can bend the rules about 'private' and 'final'.

Since you are still learning and I think you still miss some basics, I suggest you stay away from reflection for now. It's not a main language topic and experienced programmers just know to stay out of it as much as possible. It would just muddy your understanding in this stage, or distract you from more important topics.