This week's book giveaway is in the OCAJP 8 forum. We're giving away four copies of OCA Java SE 8 Programmer I Study Guide and have Edward Finegan & Robert Liguori on-line! See this thread for details.
ok the thing is like ....this according to what i know.. a static code block cannot access instance variables.. but wat if instance variables are passed as parameters to a static member function?? will the rule bend?? will a static block be then able to access instance variables?? if yes then can anyone explain why this happens... cheers nik
You cannot access instance variables from a static context, simply because to the static method the instance variables do not exist. You can pass the value of an instance variable to a static method but there would be no point. You would be better off defining a non-static method instead.
> what if instance variables are passed as parameters to a static member function?
What if the static value was loaded from a file, uplinked to a satellite, downloaded by a .NET application server and sent through a SOAP message to a process that called that static method? Tongue-in-cheek of course but the point is you do not have a reference to a static value; you have a parameter whose argument may have any number of additional references.
Here's a less elaborate example. You cannot access private instance variables from outside the declaring class. Is it bending the rule to return a reference to it?