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

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
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 we'll continue with the video so I just hope you have answered the video and now it's 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 we'll 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 let's 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 let's 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 let's 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 let's 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 don't 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 let's try to answer question can we use a static variable inside a non-static member or non sharing method yes we can't 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 let's 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 let's 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 you're not sure right now since every objects have its own value for noun 1 we don't know that this method will increment which value and that's 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 that's that's the answer because every object will have its own value and if you try to change the value using static methods that's 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 I've 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
Info
Channel: Telusko
Views: 134,948
Rating: 4.8700786 out of 5
Keywords: telusko, navin, reddy, tutorial, java, What is static and non static members, Java Tutorial for Beginners Static Variables and Static Methods, Java Static Variables and Methods, Java beginning : static and non static
Id: msXJVIecGp0
Channel Id: undefined
Length: 4min 53sec (293 seconds)
Published: Thu Mar 16 2017
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.