7.14 Why Non-Static variables does not work in Static method in Java?

non static method cannot be referenced from a static context
This is a topic that many people are looking for. https://granthamandira.org/ is a channel providing useful information about learning, life, digital marketing and online courses …. it will help you have an overview and solid multi-faceted knowledge . Today, https://granthamandira.org/ would like to introduce to you 7.14 Why Non-Static variables does not work in Static method in Java?. Following along are instructions in the video below:

so look at this code and tell me will this code will work or not so before continuing with the video just go to the comments section and mention your answer now with answer I mean will it work or not also mention the description why you think this will work or why you think this will not work and then then well continue with the video so I just hope you have answered the video and now its time to understand the answer so this code will not work and why diagonals see in this video so welcome back any assistance of energy from toys for learnings and in this video well talk about a question the question is can we use a non static variables inside a static methods the answer is no of course but then why not so if you look at this code we have a class where you have a variable which is int I and then we have a method which is public void main and if you try to increment the value of I there you can see there is an error but then why to understand this lets try to understand

different types of variables and methods so variable can be of two types not just variable but method also so you can have a static or you can have a non static members now when you say you have a static member which means if you want to call those members may be variable or the method we need a class so we have to mention a class name and then we can use the members but then if you have a non static members we have we need a object error right so without the object you cannot use the member so lets imagine this class here so we have a class a and in this class a we have two variables now one which is non static and we have num2 which is static so if you want to use num2 we just have to use the class name which is a dot num2 and your job is done but then if you want to access num1 which is a non-static variable we have to we have to create an object right so I show up getting one object admitted three objects here so when you have three

objects which is obj1 obj2 and omega-3 and lets imagine obj one has a value for noun 1 as 2 over it – as a null value for now 1 as 3 and then obj 3 has enough value for num2 as 4 so we have all this values right so every object will have its own value for num – but what about number one what about num – so num – will have a value lets say if I assign so for all the object will be having the same value for num – so if you specify the value for num – as 5 it will be applicable for all the objects because all the object which will share the same value for num – because it is a static variable right and we dont need to actually use object then we can also use the class name but then all the object can use the same value for the static variable but then every object will have its old value for instance variable right now so lets try to answer question can we use a static variable inside a non-static member or non sharing method yes

we cant do that because every object will have its own methods right example if you have two methods one is static and one is non static lets say we have one whether we just show which is non static now if I try to access the static member in the non static method is it is it will run because the static variable will be shared between all the methods so even if you calling a show method which is a non static member not sharing method it will work right is because every object can use the same strategy object or stand state static variable but what about if I try to use this all notional non-static variable which is num1 inside a static method now what will happen is to call a static method we need a class name right it is not comforting for us to use the object and lets say if I call this method which is increment so if I call increment which is us which is a static method and if I try to use num1 so question is if I say num1 plus plus and if I call the increment method

by using class name which is a dot increment now tell me which way they will be incremented is it object wiring variable or object to variable object variable youre not sure right now since every objects have its own value for noun 1 we dont know that this method will increment which value and thats a confusion because we have two different context here we have static and we have non static so we can use a static variable inside a non static method because all the members all the non static members share the same static variables but what about can we use non shellack variable inside static methods no right so thats thats the answer because every object will have its own value and if you try to change the value using static methods thats not possible so in the answer at the end the answer is you cannot use our non static variables inside our static methods so I hope you liked the video baby Ive talked about studying and non static and if you really liked it click on the like button and do subscribe and share with your friends thank you so much for watching

telusko, navin, reddy, tutorial, java, What is static and non static members, Java Tutorial for Beginners Static Variables and Static Methods, Java Static Va…
Thank you for watching all the articles on the topic 7.14 Why Non-Static variables does not work in Static method in Java?. All shares of https://granthamandira.org/ are very good. We hope you are satisfied with the article. For any questions, please leave a comment below. Hopefully you guys support our website even more.

Leave a Comment