Communiquez avec les autres et partagez vos connaissances professionnelles

Inscrivez-vous ou connectez-vous pour rejoindre votre communauté professionnelle.

Suivre

Is it possible to access non static variable inside the static block, and how?

user-image
Question ajoutée par pushpa latha ANNELA , data verification , success solutions
Date de publication: 2016/06/14

if we pas the object of  the class in the static block the we access the non static variable  in the static block

Mostafa ElSheikh
par Mostafa ElSheikh , Software Development Engineer II (L5) , Amazon

Actually, It's not possible to access a non-static variable inside a static block.

Static variable is a class variable; you can access it through the name of class without creating an object from that class.

Example: MyClass.myVariable = 5;

Moreover, you can access it through the reference of the object of that class

Example: myRef.myVariable = 5;

They're strictly the same.

More Questions Like This