Saturday, August 26, 2006

Nothing private about it

Recently I was checking up on some java fundaes that you cant find in
text books and wrote a small program to verify that "private" key word in java is a only compile time thing. Meaning at runtime it becomes a package wide access.

package somepackage;

public class ABC{
int a = 10;
}


package somepackage;

public class XYZ {
public XYZ() {
ABC instance = new ABC();
System.out.println(instance.a);
}
public static void main(String[] args) {

new XYZ();
}
}

compiled both of them and ran XYZ.

Change the access specifier of 'a' to private and
recompiled ABC and ran XYZ again and it works !.

Small tidbit but found it interesting !!.

0 Comments:

Post a Comment

<< Home