Java Puzzle – private fields

Will following program work ?

class MyClass 
{
	private int privateId = 101;
	public static void main(String[] args) 
	{
		System.out.println("Hello World!");
		new MyClass().doStuff(new MyClass());
	}
	public void doStuff(MyClass obj){
		//accessing private instance variable
		System.out.println(obj.privateId);
	}
}

Comments

  1. Many other are also able to catch it.
    Actually Any private fields can be accessed by instance of same class in the same class.

    ReplyDelete

Post a Comment

Popular posts from this blog

State Design Pattern by Example

Eclipse command framework core expression: Property tester

Composite Design Pattern by example