Start networking and exchanging professional insights

Register now or log in to join your professional community.

Follow

Can you access non static variable in static context?

user-image
Question added by Muhammed Rahshad Koyippalli , Cisco DNAC Senior Escalation Engineer , Cisco Systems - India
Date Posted: 2014/05/24

No. we can't access non static methods, variables from Static context.

Nope we cant call nonstatic variable in the static content.if we want to call it then we need to create  the instance of that class and the second solution is that we can also achieve that by declaring variable as final so it can be accessible.

qaeed ramiwala
by qaeed ramiwala , Lead Java Developer / Software Engineer , CellPoint Mobile

You can not access non-static variables (instance variables) in the static context.

In general non-static variables are instance variables which are available only when an instance of the class is created. On the other hand static context is created when the class is loaded for the first time and not during instance creation.

Noushad Thottiyil Ali
by Noushad Thottiyil Ali , Associate Technical Delivery Manager , Accolite

No, One cannot compile a code in which a Class method(Static method)  uses a object variables (non static ones). The Statics are initiated during the Loading of the class it self, but in the case of non static, they are initated only during the creation of an object. So accessing a non static from a static context  will be like acessing a non existing space in the memory.

Vivek Dudani
by Vivek Dudani , Software Engineer / Senior Software Engineer , Mastercard

No you cannot. Since, the static context (variables, methods) is loaded at class level, i.e without the need of Object creation and non-static context needs an Object reference of that class to access the variables/methods, so, non-static context is not available in static context but the vice-versa is true.

pavan kothareddigari
by pavan kothareddigari , software programmer , prime soft pvt inc ltd

no,

More Questions Like This