//****************
public class Test1 {
private int i;
Test1(int ii) {
i = ii;
System.out.println("Self: " + i);
// to avoid infinite recursion
if (i != 3) {
Test1 other = new Test1(3);
other.i++;
System.out.println("Other: " + other.i);
}
}
public static void main(String[] args) {
Test1 t = new Test1(5);
// The private field can be accessed from
// static method of the same class
// through an instance of the same class
System.out.println(t.i);
}
}
//****************
public class Test1 {
private int i;
Test1(int ii) {
i = ii;
System.out.println("Self: " + i);
// to avoid infinite recursion
if (i != 3) {
Test1 other = new Test1(3);
other.i++;
System.out.println("Other: " + other.i);
}
}
public static void main(String[] args) {
Test1 t = new Test1(5);
// The private field can be accessed from
// static method of the same class
// through an instance of the same class
System.out.println(t.i);
}
}
//****************
import java.lang.reflect.*;
public class Test{
private int i;
public static void main(String [] args){
Test t1 = new Test(1);
Test t2 = new Test(2);
try{
System.out.println(t1.getPrivate(t2));
}catch(Exception e){
e.printStackTrace();
}
}
public Test(int i){
this.i = i;
}
public int getPrivate(Test t) throws Exception{
Field f;
f = t.getClass().getDeclaredField("i");
f.setAccessible(true);
return f.getInt(t);
}
}
public class Test1
{
private String s; public Test1(String s)
{
this.s=s;
} public void testPrivate(Test1 another)
{
System.out.println(another.s);
} public static void main(String[] args)
{
Test1 t1=new Test1("aa");
Test1 t2=new Test1("bb");
t1.testPrivate(t2);
}
}