Can we access private data outside of the class directly in
java programming language? Why There is no runtime checking
in java, which leads to access the private data directly
outside of a class?

Answers were Sorted based on User's Feedback



Can we access private data outside of the class directly in java programming language? Why There i..

Answer / foobar

possible using reflection

Is This Answer Correct ?    9 Yes 1 No

Can we access private data outside of the class directly in java programming language? Why There i..

Answer / gaurav sehra

Using reflection we can see\access private data or private method of a class
u can try the below code and will easily see the desired result :-

import java.lang.reflect.Field;
class SimpleKeyPair {
private String privateKey = "India"; // private field
}
public class PrivateMemberAccessTest {
public static void main(String[] args) throws Exception {
SimpleKeyPair keyPair = new SimpleKeyPair();
Class c = keyPair.getClass();

// get the reflected object
Field field = c.getDeclaredField("privateKey");
// set accessible true
field.setAccessible(true);
System.out.println("Value of privateKey: " + field.get(keyPair)); // prints "Tokyo"
// modify the member varaible
field.set(keyPair, "Bharat");
System.out.println("Value of privateKey: " + field.get(keyPair)); // prints "Berlin"
}
}

Is This Answer Correct ?    7 Yes 1 No

Can we access private data outside of the class directly in java programming language? Why There i..

Answer / puneet

This is a compile time activities, the scopes and the access
issues are handled at the compile time and this exception is
not the subclas of the runtime exception class and doesnt go
unchecked during compile time, they are checked at compile
time and the compile time exceptions are checked exceptions
which are checked by the compiler before hand and the
example of runtime exception is say bad logic like array
having -ve size and division by zero where actual excecution
occurs in JRE of the byte code.

Is This Answer Correct ?    4 Yes 0 No

Can we access private data outside of the class directly in java programming language? Why There i..

Answer / chaitali

No,we cannot access private data outside of class in java.
There is only compile time checking and no runtime check in
java because Java supports concepts like Polymorphism and
reflection where on runtime certain decisions are taken

Is This Answer Correct ?    14 Yes 14 No

Can we access private data outside of the class directly in java programming language? Why There i..

Answer / binit

no..............

Is This Answer Correct ?    0 Yes 7 No

Post New Answer

More Core Java Interview Questions

Explain parallel processing in java8?

0 Answers  


there are N number of matchboxes numbered 1...N.each matchbox contain various number of stick.Two player can alternatevely pick some amount of stick from the higest stick containing box . The player is condidered win if there is no stick after his move.Find the final move so that the move player win. Note:In case the number of stick is equal ,pick the stick from the higest numbered box.

0 Answers   Manhattan,


what is difference between Exception and Error?

36 Answers  


what is stringtolennizer with example?

2 Answers   Symphony,


What happens if we override private method?

0 Answers  


What do you mean by singleton class in java?

0 Answers  


what is mean by overriding in which situation we wil use?

5 Answers   Atlas Systems, CSC, DCPL,


what is ABSTRACTION and what are using in real time project?

1 Answers   iGate,


Can we make constructors static?

0 Answers  


What do you mean by garbage collection used in java?

0 Answers  


Is binary a low level language?

0 Answers  


What do you mean by Remote procedure call?

2 Answers  


Categories