ابدأ بالتواصل مع الأشخاص وتبادل معارفك المهنية

أنشئ حسابًا أو سجّل الدخول للانضمام إلى مجتمعك المهني.

متابعة

Can you access non static variable in static context?

user-image
تم إضافة السؤال من قبل Muhammed Rahshad Koyippalli , Cisco DNAC Senior Escalation Engineer , Cisco Systems - India
تاريخ النشر: 2014/05/24
king bong
من قبل king bong

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
من قبل 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
من قبل 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
من قبل 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.

المزيد من الأسئلة المماثلة