This week's book giveaway is in the OCPJP forum. We're giving away four copies of OCA/OCP Java SE 7 Programmer I & II Study Guide and have Kathy Sierra & Bert Bates on-line! See this thread for details.
shantanu kaushik wrote:How can I access members of enclosing non-static class in a static nested class as non static member cannot be accessed in a static block
First, do you know why a static block can't access a non-static field? This reason is not arbitrary. The Java designers didn't just do it for style. An instance is not needed to make the static method call, so how can you get something that may not even exist?
Second, do you know that static inner classes, aka nested classes, do not need an instance of an outer class to be created? In fact, you will get a compilation error. Instances of static inner classes do not have an enclosing outer class, and hence, don't have any fields of the outer class.
So.... you want to access something that does not exist, from an instance that may or may not have been used to call a method?
If you want to do this, I recommend passing an instance of the outer class as a parameter to the static method.