Session 13- Java OOPS Concepts - Usage of this and static keywords in Java

SDET- QA11,708 words

Full Transcript

so first uh uh let us start with uh some keywords 

in Java last class we have seen this keyword and uh I'll show you some more examples and then 

we will discuss static keyword so the next one is this keyword and in Java there are so many 

keywords are there like this is one keyword static final okay and and we'll discuss one by 

one so first thing this keyword so where we have to use this keyword so before using this keyword 

we have to understand about variables there are two kinds of variables or two types of variables 

we have in Java one is called class variables class variables and the second is local variables 

class variables and local variables so these are the two types of uh variables which we have in 

Java class variables which are also called as uh instance variables and then local variables and 

uh in Java we don't have Global variables okay some other programming languages we have local 

variables and Global variables but here in Java we don't have a global variables we have just only 

class variables and the second is local variables okay let's try to understand because this keyword 

is related to this one and why don't why we don't have a global variable so that also I will show 

you so let's go with the eclipse and create new package today is day 13 say finish okay now inside this 

I'm creating new class I'll name it as this keyword and take this main method and say finished okay so in this uh if I look at this 

class and Method this keyword is a class and which contains a method main is a method right so if 

you create any variables outside of methods that means within the class anywhere within the class 

they are called class variables so for example if I create a variables like this x comma y these 

are all class variables these are class variables or we can say instance variables class variables 

or instance variables and whatever variables we created inside this method it can in case of 

main method or any other method if you create variables inside of the methods call them as a 

local variable or sometimes we'll receive the parameters here they are also they also called 

as a local variables okay and what is global variables so Global variables means the variables 

which are defined outside of the class suppose if I create variables here outside of the class here 

we call them as a global variables but in Java we cannot create anything outside of the class 

everything should be part of the class it can be me third variable whatever everything should 

be part of the class so outside of the class we cannot do anything else so that is the reason we 

do not have concept of global variables in Java so we have only class variables and local variables 

okay now uh in this main is a one method right so let me create another method in the same 

class so I have created two variables X comma Y and to assign the data into these variables I 

can create either Constructor or I can create a method anything is fine so first let me create 

one Constructor so how to create a Constructor Constructor is meant for what initializing the 

data in a variable so this keyword this is my Constructor and this particular Constructor 

we'll take I'll take two parameters let's say int a comma int B now again I'm reing a into X 

and again reassigning uh B into y so like this I have created one Constructor so if I look at 

here X and Y are the class variables and a and b are the local variables so the parameters also the 

local variables and if suppose if you create any new variables inside this method there also comes 

under local variables what is the main difference between these two is the class variables we can 

access everywhere in these methods in all the methods in the class we can access these variables 

whereas the local variables we can access only within this method they are limited to this method 

that is a difference class variables we can access in all the methods in the class whereas local 

variables we can access only within the method in whichever method we created only within that 

method we can access the local variables okay now I have just created one Constructor and assign 

the data into X and Y after that I will create a normal method just for printing the uh values of X 

and Y so I can create one more method void display and in this I will just print the values of X and 

then I will print the value of y okay this is an additional method I have created just for display 

the data of X and Y so so far everything is clear so we have just created one Constructor which 

will take two inputs and assign the same data in the X and Y and then I printed X and Y values 

using display method now come to the main method so how to access all these things from the main 

method I will create an object of this keyword let's create object this keyword t equal to new 

this keyword so once you created an object what will happen this will try to inor Constructor 

right so as soon as you created an object because this class contains a Constructor so it is trying 

to invoke The Constructor but if I look at here the Constructor is expecting two parameters but 

we have not passed anything here so we have to pass two parameters so let us pass two parameters 

let's say 100 comma 200 so two integer parameters I'm passing so at the time of object creation this 

Constructor will automatically invoked and 100 and 200 will be received in A and B and then again 

reassign them into X and Y so after that by using this object I will call display method so that it 

will display the values of X and so everything is clear when I execute this you will get the values 

of X and y200 perfectly fine so now if you come back to this part X and Y are the class variables 

here A and B are the local variables now if I use same variables like same names class variables and 

local variables then what will happen suppose here also I will say X and Y and here I will assign X 

into a X and Y into y if I do like this then what will happen when you run the Java program it will 

give you 0 0 so why it is giving 0 0 means okay here we are using same names local variables and 

class variables and internally what we are doing we are assigning this x value Val into X Y value 

into Y when you do like this it will not throw any error it will run the program there is no 

syntax error nothing but you will get incorrect output so these type of uh errors called logical 

errors these type of Errors called as a logical errors in Java there are two kinds of Errors 

syntax error logical error syntax error means what suppose if you is semicolon or if you miss 

some bracket write instead of lowercase character if you write uppercase character so that comes 

under the syntax errors but this is not syntax error logical error means program is correctly 

executing statements are correctly written but the output which we are getting is incorrect so 

the logic is not correct so why we are getting 0 0 because when you assign X into X and Y into 

y this method or Constructor will confuse like which when it's a simple x and x equal to X which 

X is belongs to to local variable which X is class variable it doesn't know that similarly which Y is 

class variable which Y is a local variable doesn't know that so what simply it will do is it will 

assign the default values in X and Y so it is 0 0 the default value of integer variable is if 

you're not specifying any value into X and Y by default 0 0 integer variable value is zero so it 

is getting simple z0 this is a problem when I use same name class variables and local variables now 

to overcome this problem still I want to use the same variable names so what we can do is we can 

simply use this keyword so this is representing a class so this do xal to X and this dot yal 

to Y so now when I said this keyword this do YX means what this is representing the class now 

this x is belongs to the class this Y is belongs to the class okay now we can differentiate like 

this so wherever you see this keyword uh this keyword this is always representing the class okay 

now it is differentiated so it will understand which one is class variable which one is a local 

variable so this time we will get the output like this so this is the main usage of this keyword 

to differentiate the local variables and class variables we can use this keyword and not only 

in the Constructor even it is a method it the same process suppose for assigning the data into 

the variables instead of Constructor I will create uh let's say I will create one method user defined 

method let's say void I call it as a set data okay same thing I'm taking two parameters let's say 

in a comma in B now assign the data and uh in y I will assign B so this time we created user defined 

method I commented this Constructor so this will not work so now we need to create a normal object 

without parameters so this keyword P equal to new this keyword okay so just normal default 

Constructor and then before displaying the data we have to call that method because it is a method 

we have to explicitly call through object if it is a Constructor at the time of object creation 

it will automatically execute but if in case of method we have to call it through object so DH dot 

set data and here we have to pass the data let's say 100 comma 200 so again this is the same thing 

so when I execute this and set data method is got called and 100 and 200 is assigned to uh A and 

B and again reassigned to X and Y and we got the values and suppose if I use same variables again 

same problem you will get let's say X and Y I'm using here here also X and Y and then save it and 

when I run this again you'll get 0 0 because the local variables and Method variables are same 

so now we need to use this keyword if there's ax and then here also this do y so whatever the 

properties are applied for Constructor almost the same thing is applicable for method also so you 

got a values so this is how we can use uh this keyword simple concept so this keyword will be 

used uh to differentiate local variables and class variables so this keyword is always representing 

the class this keyword is always representing the class or object okay so is this clear everyone so 

where exactly we want to use this keyword and in Java we don't have a global variables concept 

if anybody is saying Global variables that is incorrect terminology in Java because in Java we 

don't have a global variables Global variables means we have to define those variables outside of 

the class but without class we can't anything else in Java within the class only we can do everything 

so Global variables concept is not there in Java only the class variables and Method variables 

but in Python we have a global variables concept outside of the class also we can Define the 

global variables but in Java we do not have Global variables we have only two types of variables 

class variables or local variables okay this this is about this keyword now we will discuss 

one of most important keyword and we are so far we are using it but we don't know exactly what 

it is that is static keyword static key but this is a very important and uh little confusing topic 

also and we need to carefully listen this the main class variables are also class variables no the 

main class variables the Ables which are defined inside this main method or again local variables 

see the variables which are defined inside this method main method if I create some variables 

here they also local variables for the main method okay and if you want to access is X and Y 

you can access inside the main method through the object through object we can directly access these 

variables right so these are again class variables and whatever variables we created inside the 

main method we can call them as a local variables you can use different variables also nothing wrong 

in that okay but the thing is if you're using a different names for the variables the maintenance 

will be difficult you can't remember okay if it is Small Program you don't see any issue but if 

you're writing a bigger program so many classes so many Constructors so number of variables will 

be increased so many variables you have have to write okay and that is the reason if you say same 

variable names no problem even if you are using different variable names still no problem we can 

still use it nothing right nothing wrong in that okay right so right so now let us see one more 

keyword called Static listen this very carefully because this is a little bit confusing topic 

static keyword and if you f Focus you can easily understand static keyword so before understanding 

static keyword so first thing the static keyword can apply for variables and also methods we 

can apply the static keyword for variables and methods also we can create static variables we 

can also create static methods simple when you put the static keyword in front of the variable 

that variable becomes a static variable similarly if you put this static keyword in front of the 

method which is called as a static method so we have totally four different type of variables 

and methods static IR respective static like the static variables and static methods nonstatic 

variables and nonstatic Method what is non-static non-static Method non-static variable simple 

if you specify static in front of the variable that becomes a static variable if I don't specify 

static that's a non-static variable same thing if you specify static in front of the method we call 

it as a static method and if you don't specify static we call this a non-static method okay 

simple so whatever method we created simple here in every class these are all non-static because we 

have not specified any static and in case of main method here we can see static keyword so this is 

a static method I don't specify static these are normal methods nonstatic methods okay remember 

this now we need to understand something when we have to use static keyboard and why we have to use 

static keyword and what is the benefit of using static keyword so let us try to understand this 

uh with an example and then we will see practical implementation of static keyword so I'll explain 

this with the scenarios different scenarios and you can assume those scenarios and you can and 

try to understand this and also in the middle of something I will ask also ask some questions 

and you should answer that okay just a second just so let us say I have something called a 

new class here this is my employee class okay so in employee class I will Define some 

variables let's say employee name employee ID salary department number along with this I will 

create some methods let's say bonus calculation method so I can create some methods this is one 

class which we have employee e name Eid salary department number and bonus and so on so basically 

the class contains a variables and methods now for this particular class you can create n number of 

objects there are so many objects multiple objects we can create so I'm creating multiple object 

let's say this this is my one object let's say this is my emp1 object and this is emp2 object 

and this is emp3 object I can create n number of objects right so these objects are independent 

even though these objects are derived from the same class these objects are independent because 

these objects will occupy a different space in the the memory so emp1 emp2 emp3 totally different 

and as soon as you created an object what will happen normally the object will acquire their 

own copy of the variables and methods from the class so what is emp1 object is having now emp1 is 

having same EMP name Eid salary department number and then bonus and here emp2 also having same 

variables eame Eid salary department number and then bonus method same emp3 is also having same 

variables eame Eid salary department number and bonus so like this every object is having their 

own copy of variables and methods acquired from the class this is a normal scenario so suppose 

let me tell you a different case when you create an object the object is independent and object 

will acquire their own space and it will acquire their own copy of the variables and methods 

from the class after that what you will do we will operate the objects right once you created 

an object we will always work on the objects not on the class so after creating an objects then 

what you will do by using this reference variable object varable we will try to assign some data 

into the variable and then we will perform some operations let us say here I will assign some 

data e name some ex Eid some number salary some number DPT number some 10 like this and here 

also emp2 also I will assign some data like this employ 102 salary something and dpit number 

20 like this and emp2 also I can assign some data this is the next task so once you created your 

class we can create multiple objects once you created an object through the object variable we 

can access all the variables and method which are belongs to specific object so far this is clear 

now my scenario is suppose if these employees are belongs to same department number let's assume 

this scenario okay these employees are belongs to same department number which is 10 okay let 

us assume these employees are belongs to same Department which is 10 department number 10 

in that particular case for every EMP object we have to assign the department number let's 

say here I have sent 10 here also I'll asend 10 here also I asend 10 so let us I have 100 objects 

like this in every object we have to assign the data along with other variables even department 

number also we have 10 right if you do like this there are two problems so the one problem is the 

same department number we are repeating in every object in every department number is same right 

every object or every employee Department n same even though it is the same we have to assign there 

is no other way because if you assign department number for one object that will not reflect on 

another employee object right because objects are independent so here we have to assign 10 here 

we have to assign again 10 here we have to assign 10 every object we have to assign same number 

if you do like this there are two problems one problem is suppose tomorrow these people are moved 

to some other department let's say currently all these people are belongs to department number 

10 now they have moved to department number 20 for example then what will the pro what is the 

problem we need to modify this number in every object we have to update this department number 

value in every object if I create 20 objects in 20 objects we have to go and change it that is a 

one problem modification or we can say updation modification or updation is one biggest problem 

because if these people are moved to some other department number we need to go and change each 

and every department number in object levels that's the first problem now the second problem 

so here department number 10 we occupy let's say 1 KB size in memory and here also 1 KB here also 

1 KB totally how much size it is occupied that's a 3 KB size is occupied because every object is 

independent every variable needs a memory some amount of memory required in space required in the 

memory right so as soon as you assign data into the variable definitely it needs a memory and this 

will need another memory this will need another memory that means even though the values are same 

because those variables are belongs to different objects it will occupy a different spaces in 

the memory that means memory also wasted memory also memory is also one of the problem because 

because of duplication why memory problem because of duplication so same number we are assigning uh 

in variables in multiple objects so this is also occupying different space in the memory because of 

duplication the memory is also one of the problem this is the current scenario and assume that 

if these people are belongs to same department number then only these two problems which we have 

right now in this particular case what I can do is how to solve these two problems first is wherever 

you specify this variable department number in the class we can directly assign the data here because 

this value is common across all the objects right this value is same across all the objects so if 

any value is the same we can directly specify that in the class itself hard code here itself 

right and make this variable as a static make this variable as a static then what will happen 

is whenever you make any variable as a static only for this particular variable a separate memory 

will be created here D number will be stored only for this particular variable a separate memory 

will be created and here the 10 will be stored and another thing is this particular variable 

becomes the common variables across multiple objects that means this particular D number 10 is 

a common variable across multiple objects normally there will not be any common thing between objects 

right every object is independent as we already understood earlier every object will occupy 

different memory and they are independent there is no relation between these objects but if you make 

any variable or method as a static that method or variable becomes common variable or common 

method across multiple objects okay that's the first point so we can make that variable uh we can 

directly assign the value to this variable and we can make it as a static as soon as we make it as 

a static that variable becomes the common variable across multiple objects and one more thing if 

you make this variable as a common and assign the data do we really need to assign the data here 

through the object no need this is not required okay because we already assigned the data and the 

department number that variable is a common which is there in emp1 which is same thing is there in 

emp2 same thing is there in emp3 how many objects we created in every object this D number will be 

there already so if it is already there you no need to again assign the data into the dpd number 

for every object okay so once you specify this variable and uh you understood the problem right 

what is the problem if the variable values are common across multiple object what is the problem 

here is modification duplication is a one problem and memory will occupy to solve these two problems 

we will make this variable as a static and assign some data into the variable because that Valu is 

common across all the object so we can directly assign the data into this variable and make it 

as a static what is an advantage of static here is this variable becomes common across multiple 

objects and now modification suppose if these people are moved to some other department number 

so I no need to modify in every object directly go here and change it here make it as a 20 that's 

it that will reflect in every object why because this variable is a common across all the objects 

okay and not only here you can still have 10 no problem you can change this value by using any 

of this object okay because as soon as you make it as a static this this is a created separate 

memory right so this variable we can update by using any of this object so if you let's say emp1 

do dpit number equal to 20 that will automatically reflect in emp2 and emp3 also and if you update 

this variable like emp2 department number 20 that is also reflecting emp1 as well as emp3 same 

thing if you update this variable like emp3 do department number 20 that also reflect in emp2 and 

emp1 also that means if any changes happened in this variable we can directly make the changes in 

the class level or we can make the change of this variable by using any of this object any object 

you can use so that will reflect in every place so that is a main advantage of static keyword 

okay so when I create a variable as a static that variable becomes a common variable shared variable 

we can say common or shared variable between all the objects so that variable you can update you 

can change the value of the variable by using any of this object that will reflect in all the other 

objects also if you make the change by using EMP one object that will automatically reflect in 

emp2 and emp3 also if you make the change by using emp3 object that will automatically reflect 

in emp2 emp1 also why why it is happening like this because it is a common variable or it is 

a shared variable between multiple objects so we can update this variable directly by using 

a class or we can update this variable by using any of the objects so that is the main advantage 

of static so when we have to go with the static so by using the static we can overcome these two 

problems so modification or updation if you modify in one place that will automatically updated in 

all the objects so this problem first problem is solved but how memory will be solved memory 

problem when you add this data in every object level let's say here 1 KB 1 KB 1 KB now when I 

make this as a common variable here just only 1 KB here we know need to asend here we know need to 

ascend here we know need to ascend so instead of 3 KB we just used only one KB only one place 

so memory is also saved so to overcome these two problems we can use static keyword but there 

are some restrictions when we have to use static keyword when we should not use static keybord 

if you find any data is commonly required or if any data is a common across multiple objects if 

any data is common across multiple objects then only you can create that variable or method as a 

static otherwise don't do that okay don't do that so if these values are different from one object 

to another object don't use static keyword it is not recommended just create a normal variables 

and assign the data into the variables by using an objects but if you find any data or any 

variable data common across multiple objects then make that variable as a static same thing if the 

method implementation is same for all the objects then make that method as a static otherwise don't 

do that okay so only few members shift to other department in if you have such type of scenarios 

don't use stattic keyword that is not recommended so static keyword is recommended only if you 

have a common data across multiple objects if not don't use a static keyword okay static keyword 

is recommended only if you have a duplicate data across multiple objects even if one object is 

different value don't use static so first two objects are having 10 another object is having 

20 another object is having 30 department number don't use static just create a normal variable 

because this value is not common so we cannot make it as a static if you make it as a static that 

becomes a common variable if you do change in one place that will automatically reflect in all other 

objects so your case will not work okay so static keyword will be used only if that variables values 

are common across multiple objects X if the values are not same not common don't use static keyword 

so is is everybody understood this is a scenario but this scenario I'm just taking just for an 

example don't think all employees are belongs to same Department different belong employees belongs 

to different different type of departments there's a different case I'm just telling the case just 

scenario have taken here in case any variables are common the values of the variables are common 

across all the objects then make them as a static or else no yes you can put employer name State 

country yes so those are normally common right employee employer name so whatever employees 

are working in the organization the organization name is always same state country yes they are 

exactly the same so in such case you can make them as a static because the same values will be 

there for every type of object but if the values are different salaries is different we should 

not make as a static employe ID is different we should not make it as a static okay so this 

is a scenario where we have to use static where we should not use static is this clear everyone 

so please confirm in the chart box so now I will show you some examples but when I create a static 

variables or static methods there are some access level restrictions are there so which we need to 

understand now okay fine so if anybody ask you in interview when static keyword is required what 

is the use of static you should explain with some example like this right so now let us see some 

example so we have uh static static keyword we can apply for variables and methods and also if 

you don't use static we call them as a non-static variable non-static methods if you apply static we 

call it as a static variables and static methods okay now so when I use static keyword there 

are some access level restrictions are there we cannot directly work with the static variable 

static methods just like a normal variables and normal methods so we will try to understand 

that so go to eclipse and uh I will create new class let say static demo and also 

I'm taking main method uh in the same class and remove this main method now 

can you guys tell me main is a static method or non-static method main 

is a static method or non-static method main method nonstatic method or static 

method it's a static method because you can clearly see static keyword in front of the 

main method right so this is a static method okay all right so now in this particular class 

I will create uh some variables like static and non-static similarly I will create some methods 

like static methods and non-static methods let's do that so I'm creating one variable called in 

and I can directly assign the value to this and this is a normal variable non-static if you want 

to make this variable as a static you have to say static keyword now this becomes static variable 

static in AAL to 10 so this is what static variable this is static variable and int b equal 

to 20 and here I have not specified static keyword so this is a nonstatic variable this is nonstatic 

variable similarly I will create one method let's aoid M1 this is one method I'm creating in front 

of this I will say stat IC then this becomes a static method now inside this I'm writing a 

simple message this is M1 static method so this is a static method because we specify the static 

in front of this method so this method is a static method and also I will create one more method void 

M2 and uh here I will just this is just non-static method nonstatic nonstatic method and here I will 

just print something this is M2 nonstatic method okay now if I look at this in the class I created 

two variables one is static variable other one is non-static variable similarly I created one static 

method other one is a non non-static method and anyway main method we already have which is also 

static method okay now if you want to access this if you want to access this variables or methods 

normally what we have done so far to access the variables and methods from the class what we 

have done we have created an object of the class and through the object we're able to access 

everything right through the object we're able to access the variables and methods and everything 

and where we have created that object inside the main method inside the main method only we have 

to create an object only through the object we are able to access everything right so far we have 

done like this now what I'm saying is the static variables and static methods we can directly 

access without creating any object okay that means where we are creating an object in the main 

method right so main is what the static method so the static methods can directly access static 

variables and static methods there is no object required there is no object required that's the 

first point static methods can directly access static variables and methods okay that's the first 

point so the first point is what static methods can access static stuff directly that means 

static stuff in the sense what static variables and static methods and static methods can directly 

access static stuff directly without creating any object without creating any object what does 

it mean main is a static method so the main method can directly access static variable and 

static method without creating any object object without creating any object that's the first point 

because why main is a static method variable is also static the method also static everything is 

static so we can directly access without creating any object let me show you so here I'm trying to 

access a how to access normally when you create we create an object through object we will access but 

here I'm not creating any object I can directly get the value of a see I'm directly accessing I'm 

not created any object similarly I will directly call M1 method I'm not created any object if you 

run this code it will give you the results so you can see the value of a is got printed and the M1 

method is got executed so why I'm able to call these two directly because main is a static method 

a is a static variable M1 is also static method everything is a static means the static method or 

methods can access static stuff directly without creating any object this is the first point okay 

static variables static methods can access static stuff directly this is an example okay let us try 

to access nonstatic stuff and see the difference so here just like a I'm trying to access B and 

just like M1 I'm trying to call M2 directly it is giving an error what is an error it is giving 

I put the cursor here it is saying cannot make a static reference to the non-static field B it is 

clearly saying you're trying to access non-static variable it is not possible similarly if you look 

at here M2 cannot make a static reference to the non static method M2 from the type static demo 

and also it is giving a suggestion also what is the suggestion change M2 to static if we make M2 

also static then we will able to directly access similarly in case of B also if we make b as a 

static then we will able to directly access right so what does it mean static variables and static 

methods we can directly access from the static methods without creating any object this is the 

first point this is the first point static methods can directly access static variables and static 

methods and this is not possible so these are direct access for non-static is not possible okay 

so here cannot access why because B is nonstatic variable similarly M2 also cannot access M2 

also so directly cannot access cannot access why because M2 is nonstatic Method nonstatic 

method so this is the first point is this clear everyone the first point don't go beyond this 

I will show you all the scenarios first point is clear or not static methods can access static 

stuff directly without creating any object and if I look at here main is a static method e is a 

static variable M1 is also static method so I have not created any object I'm just directly 

access a value I'm directly call M1 method so we are getting an output but when you're trying to 

access B and when you're trying to access M2 it is giving an error why because they are non-static 

okay this is the first point now the second Point static methods can access static stuff 

directly non-static stuff we cannot access directly the second point is static methods can 

also access non-static only through object through the object it is possible second Point static 

methods can access non-static stuff through the object what is the difference between first 

point and second Point static methods can access static stuff directly everything is a static so 

we are able to access directly without creating an object but in the second case static methods 

can also access nonstatic but it is not direct through object we can access through the object we 

can access there is no direct access now observe here so when I'm trying to access B and M2 

directly we are not able to access because they are the non-static variable nonstatic method 

we are trying to access from the static method so it is not possible now the second Point what I'm 

saying is we can also access nonstatic through the object through the object so now let us 

create an object of static demo class static demo SD equal to new static demo now I created an 

object through this object we can access now we can say system. Pinel and SD DOB we can access 

similarly SD do M2 we can call okay so we are accessing from static method only but through the 

object we can access non-static variable you can access nonstatic method through the object okay 

now when I run this you will get the result of B and also it will call M2 method okay two points 

are clear so static methods can directly access static stuff without creating any object but 

when you're trying to access non-static stuff only through object it is possible we cannot 

directly access only through object it is possible these are the two points now the third 

Point not we first two points we we are able to access we are trying to access from the static 

method which is main method now the third Point nonstatic methods can access everything directly 

nonstatic methods can access everything directly non-static methods I'm talking so I will create 

one more non-static method here let's a void M this is non-static Method this is nonstatic Method 

so what I'm saying in the third Point non-static methods there is no restriction all the variables 

all the methods we can directly access it can be static non-static no matter so I can just observe 

so this is M now here I'm trying to access A and B also can system. pin a I'm able to access system. 

Pinon B I'm able to access I can call M1 access M2 also I can call see this I have not created any 

object first of all we can't create any object in the non-static methods so objects will be created 

only in the main method so in any other methods objects we cannot create okay so when you're 

trying to access the variables and Method from the non-static we can directly access there is no 

restrictions for that everything we can directly access from the non-static method but when you're 

trying to access from the static method there are restrictions so from the static methods we can 

access static variables and methods directly and we can access non-static stuff through object 

but when you come to the non-static methods we can access static non-static variables and 

methods directly there is no restrictions for the non-static methods that's the third Point 

non-static methods can access everything directly but how to call this non-static method where we 

have to call this where we can call this from the main method only and again if you want to call 

this from the main method do we need object or not yes because this is a non-starting method m 

is again non-starting method we need an object right so SD do M so when you call Y method then 

what happens it will first print A and B values then it will call M1 method again method can call 

another method okay we called m method internally it will call M1 and again it will call M2 method 

okay so let me just uh command this and execute only this one only this one I'm going object is 

needed so object I create so now observe I'm just calling only M which is non-static Method so we 

got everything so first we printed A and B values 10 and 20 printed then we call M1 method this 

is output of M1 then we call M2 this is output of M2 so that means non-static method can access 

everything directly there is no restrictions but only the static method is having restrictions even 

in static methods also static variables and static methods we can directly access no problem but 

only the problem with the non-static variables so we have to create an object only through object 

we can access non-static variables and non-static methods so these are the three points everybody is 

clear or not please put in the chart window three points very very important when you're working 

with the static variables and static methods you have to remember these points so wherever 

you see static variables and static methods you can directly access them from the main method 

there is no second question okay wherever you see static variables and static method you can 

directly access them from the main method main is also static method so we can directly access 

without doubt okay but when you see nonstatic variables or non-static methods then only you have 

to create an object and through the object you can access and that is a point okay so these are the 

three points which we have to remember when you're working with the static now I'll show you one 

more thing just observe in this example I put everything in one single file right so static 

demo contains the main method also main method also there in the same class now I will separate 

this main method in another class just observe I'm creating another main class let's say static 

main class static Main and just observe this same main method just copy from here and I will keep 

this main method in another class let's comment this okay we are commenting this 

so now this time I have separated main method in the another class now just 

observe the first Point what is the first point static variables and static methods we can 

directly access from the main method right but here you can see it is giving an error when you 

have main method in the same static demo class we we don't have any issues we directly able to 

access because a and M1 are static main is also static we are able to directly access no problem 

but when you separate main method into another class static main class here it is giving an 

error what it is saying a cannot be resolved to a variable similarly M1 is undefined for the 

type static main so here it is started giving an error so why it is giving an error this is the 

main point so when I use when I try to access static variables and static methods within the 

same class you using main method we are able to access directly no problem at all but when you 

separate main method in another class when I'm trying to access directly it is giving an error 

the problem is this is a separate class inside this main method I'm directly referring a and M1 

so this main class doesn't know about this a and M1 from where we are calling them where we have 

defined them right if you create them in the same class then it can know that just like a previous 

example in the same class we created this a and M1 so it is able to recognize easily but when you 

come to the separate class here this static main class is not able to recognize these two it's 

don't know exactly from where they are coming from okay then how can we solve this problem 

just to specify the class name here also we can directly access no problem but we have to 

explicitly specify the class name so from which class we created this variable static demo class 

so here we say static demo do a static demo. M1 this is a point so when you have main method in 

the same class class you can directly access even class name is also not required but when you 

trying to access in another class you have to specify the class name do variable still object 

is not required here also we have not created any object to access the static stuff still we 

able to access but we have to explicitly specify the class name okay clear everyone so but if you 

have main method in the same class no problem you don't need don't even specify the class name you 

can directly access but if you separate the main method in another class you have to specify the 

class name then variable class name do method so this will work in both the places we are able to 

access directly here also we are able to access directly here also we are able to access directly 

only the difference is because this main method is in the same class we don't need to specify the 

class name here here but here the main method is there in the separate class so we have to specify 

the class name so we got the output this is the one difference and when you come to the non-static 

stuff there is no problem so non-static stuff we can directly anywhere we are creating an object 

right so we can create an object through object we can access B and M2 so when I run this Java 

program you will get an output from there so there is no problem with the non-static stuff even 

this also you can call directly sd. M because we anyway at the time of creating an object we are 

specifying the class so don't problem there is no problem with the non-static only problem with the 

static so when you separate main method in another class you have to specify the class name do 

variable name class name do method name especially for static other things not required it will work 

as it is okay is this clear everyone so remember this point so when you're working with the static 

keyw all these points are most important so when you make private you can't access anywhere else 

only within the class we can access that's totally restricted access so don't try to use private 

keywords every time okay and that is also not important for us we make everything as public by 

default so when you make private the variables or methods we cannot access anywhere else within the 

class only we can access so here it doesn't make any sense okay so everybody's clear all three 

points So based on that I will ask one question if you understood static keyword clearly then you 

will able to answer that question okay now so you guys can tell me the answer now I will give you 

simple example I have one simple class called test okay in this class I will have have one uh string 

variable string s equal to welcome and now this is a static variable this is a static variable now 

my requirement is I want to access this variable outside of this class that's my first requirement 

I want to access this variable outside of this class and once you access I want to find length 

of a string because it's a string variable which contains a string value so I can find length of 

a string two requirements first step I want to access these variables outside of the class after 

accessing I want to find the length of a string okay so then how can we write the statement first 

how to access the variable from the test class outside of the class class name do variable okay 

now we are able to access because s is a string variable can we have apply all string Methods 

on the S length method substract con all string related methods I can apply because which is a 

string variable so if you want to find length of a string which method you can use GTH right this 

is a complete statement and if I look at this what is test here test is a class what is yes yes is 

a a string variable also static and what is the length Here length is a method predefined method 

which is already there in the string class because s is a string variable I can apply all string 

Methods on yes finally this will return return length of a string is this clear everyone this 

is clear everyone yes now you tell me this one I have pre different class in Java which is called 

system class okay system class in this I have one variable called out I have variable called out 

so the type of the variable is what print stream type just like a string here here we have a print 

stream and this is also a static variable this is also static variable and this doesn't have any 

value it's a simple one variable now I want to access this out variable from the system class 

how to access how to access I want to access this out variable from the system class out is 

a static variable system dot out so because out is a print stream type can I apply all methods on 

out variable which are whatever methods are there in the print stream class I can apply all those 

methods in the out variable right so string class contains length Carro substring conat all those 

things Sim similarly print stream class is also having print print Ln methods so here I can access 

print or print Ln right now what is this system. out. println what is system here system is a 

predefined class in Java out is what it is a print stream of variable of static type what is print Ln 

and print the methods which are belongs to print stream class now you understood what is system. 

out. print so far we are using it almost in every program we are using system do do do print so 

this is how the system do do do Pell comes into picture okay but normally methods may take 

parameters may not take parameters right print will not take any parameters this again 

overloaded method you can pass parameters you may not pass any parameters whatever is there 

in the print lent that will just print in the console window that's it so this is all about 

system. out. pintel so in the system. out. pintel system is a predefined class and which 

contains out variable of print stream type and static variable which is already there in Java 

so now we can access this out variable from the system class system.out dot out is what print 

stream type so I can apply all the methods on the out print Ln print these methods are 

belong to print stream just you have to simulate with this Here length is related to 

string class right same thing here print and print is belongs to print stream class so 

if you understand this one you can easily understand this one so what is system. out. 

pinell if anybody asking you ask you in the interview you have to explain this system is a 

predefined class out is a a static variable of a print stream type print Ln is a method which 

is a part of print stream class here test is a class yes is a string variable of static length 

is a method which is belongs to string class similarly here also system is a class out is a 

print stream variable of static and print method is belongs to print stream class print stream 

is a predefined class in Java just like a string that means it both are not same okay 

string is a different print stream is a totally different both are predefined 

in Java they're already there Sprint stream is also there just like a string 

and string contains a different type of method similarly print stream is also contains 

a different type of method print and print is the most popular methods which are there 

in the print stream class so that's how system. out. pintel and command come into 

picture so we so far we are using it but we don't know exactly what what it is now we have 

understood what exactly it is what is system. pint understood everyone so what is system. 

out. pint if anybody ask you in the interview you should able to explain with this example if 

you understand this you can easily understand this this is related to static keyword so that's 

the reason we haven't discussed this in the previous classes so now you have understood 

about the stat keyword so that's the reason I'm just introducing this system is a predefined 

class in Java out is a static variable print Len is a method which is belongs to print stream so 

that's how system. out. printen statement is got formed any doubt in this anyone this 

is most important and very basic also if you understand this example 

you can easily understand this one shivaji are you clear print stream is just a a class just 

like a string print stream is also another class which is already there in Java okay 

now let us understand something else we are using main method in every class so main is the 

actual method from where execution will start jvm always look for the main method to start 

executing your program in whichever class you have a main method you are trying to execute 

that class if you don't have a main method in the class you cannot execute that class 

right so now we will try to understand what is main method how to write the main method 

public static void Main and which takes par parameter string uh arcs a parameter right 

this is the main method we are using every time system class what is the 

confusion here system is a predefined class system is a just a class name which 

is already there in Java I'm just writing here so everything is already there in Java 

predefined they're designed by Java itself and we know need to understand what exactly 

they are and all those things we just trying to understand the statement system is a 

predefined class out is a static variable print Ln is a method which is a part of print 

stream class that's it what is the confusion you have whenever you create a class start with 

a system yes is just capital letter similarly system here we always start with the uppercase 

character because this is a class class names will start with uppercase character every time 

okay now let us try to understand public static void main so far we are using this method but 

now we'll try to understand in detail what is public what is static what is made void Main 

and other parameters okay so in public static void main first keyword is Public public means 

this this method accessible everywhere in the project so this method is a public public means 

completely open there is no limitation in Access everywhere in the project every package in the 

project wherever you have a main method you will able to access that is the reason they make as 

a public and static means what the main method is a common across all the classes all the objects 

everywhere so they make main methods common that's the reason they make it as a static void means 

what the main method will not return any value so wide string ARS means what it a array arguments 

so this method can take array as an argument array parameters also argument so that's the reason they 

make string args and the main thing is jvm Java virtual machion we understood at the beginning 

right jvm is a main responsible for executing your program so jvm Java virtual machine always look 

for this method in your project so in whichever class wherever you have this main method the jvm 

will go there and directly execute that particular class okay so normally how we have to handle this 

is let us say you have a project structure so you have n number of of classes let's say C1 C2 C3 you 

have created n number of classes in your project in every class we don't write main method right 

different classes are there and all these classes we will control from one class main class here 

we will have a main method so jvm when I execute this project so this is everything is one single 

project this is all one single Java project so when you run this project directly the jvm we look 

for the main method wherever it is in which class the main method is available in in throughout the 

project there are so many classes are there along with the main class so in which class the main 

method is available it will go and search for it that's the first thing so wherever it is find 

this main method in the main class execution will start from here so here we will create all the 

objects of all these classes then we will operate through objects okay so public why they're making 

as a public because the main method can be there anywhere in the project so jvm should able to 

access main method anywhere from this project so it should be public and static means what it's 

a common across all objects that means what this main method should be there common wherever you 

created main method you should able to jvm should be able to access directly stating means what 

direct access right so when I when jvm trying to access main method it will directly access no 

object is required that's the reason they make it is a static so main method jvm directly can call 

without creating any object without referring to any class how it is possible because of static 

keyword and void means what this main method will not return return any value that's the reason they 

make it as a void and Main is a name of the method and string is a type and args array this single 

Dimension array which will take as a parameters so that's how this method is got designed public 

is an access modifier static means is a keyword that represents we can jvm directly call this main 

method without creating any object and void means there is no return value for the main method 

string args is nothing but a string array it will take as a parameter R is just a variable 

array variable you can put X Y whatever you want anything is accepted is just like a single 

dimensional array we'll take parameter okay that is the meaning of public static void mean now I 

will tell you something here you should tell it is valid or not okay normally how we will write a 

public static vo this is the right syntax this is the valid syntax public static void main if you 

write main method exactly in the same way the jvm will able to recognize and jvm will start 

executing the main method suppose can I write main method like this suppose let's take the 

same main method what I will do is here I will keep this static keyword in front of the public 

is it valid or not jvm can recognize this method or not and runtime jvm can recognize this method 

or not I just change these two keywords I just interchanged is it valid or not this statement 

this method still it is valid still it is valid why because we the keywords order is not important 

okay the jvm is always look for this main method is public or not static or not that's it 

here it is a public and static here it is also public and static condition is satisfied 

right so jvm will able to recognize this main method so these keywords we can interchange 

like this this is still valid this is still valid okay now I'm writing the main method like 

this this a first case this is the valid now this a second case this is also valid just we change 

static and public interchanged the keywords but still the keywords are there now the third case 

public void static mean string arcs valid or not this is invalid okay why it is invalid 

because the return type always should should be in front of this method okay return type 

you should not separate so whenever the method name start before that void should be there 

like this void meain void main so the return type always should be before this name 

of the method it should first comes so this we should not separate anywhere else 

okay so we separated void here so that is the reason it is involved it is incorrect 

syntax so before this method written type must be there but here before this method 

what we specified static that's the reason it is not allowed so return type must be there 

before method this is invalid now the fourth one void mean string asks okay 

public stat static valid or invalid is it valid or invalid it is invalid it is invalid okay because 

after closing this method you should not write anything else okay after this closing this method 

you should not write anything else whatever you want to specify that should be beginning of the 

method after closing this parameter bracket you should not write anything immediately the bracket 

should start after closing this bracket the curly brace should start okay in between you should 

not put anything else so that's the reason this is invalid it is invalid now see this public 

static void Main into a bracket is it valid or not public static w mean in a bracket valid 

or not okay this is still valid method why jvm will not execute the code from here J jvm 

is looking for different type of method what type of method jvm looking for this one string 

arguments it is looking for but when you write a method like this this is not actual main method 

but it is just like a treating as a normal method in overloading concept we created multiple 

main methods right so this is another type of main method not exactly main method which 

we are expecting got my point so this is again still Val valid method but it is not actual main 

method which we are expecting this is not exactly the same as other main methods it is just like 

a normal method jvm will not recognize this okay whatever jvm recognizing this is not that method 

got my point this is just like a normal method so observe this in the class we can create multiple 

methods right like say main one method main with two parameters main with the two parameters okay 

then Main method with the string ARS like this this is the method actually jvm will look for 

but whatever method here we have written the parameters got changed the DAT is got changed 

so this will not exactly match with the string argument so jvm will not look for this method 

but still this method is valid because this will treat as a normal method not a actual main 

method guys you understood any confusion here yes after writing this method still you have to 

write another actual main method because this method cannot recognize by jvm jvm will treat this 

method differently it will teach this main method just other type of main method not an actual main 

method from where execution will start so if you write this main method again you have to write 

actual main method so this main method again you have to write okay because JM will look for 

this one not this one but still it is valid guys any confusion you have understood why it is valid 

yeah this is still valid method overloaded method that's it it is another overloaded method because 

we can have any number of methods with the main right but syntax is different parameters should 

be different so jvm always look for main method only if you have this type of syntax if you do 

some small change it will not accept these two you can just interchange public static but rest 

of them exactly should be there I me this also you can interchange you can put ARS before this 

bracket this is also fine and you can change this parameter this is also fine so a also you can 

find finally it will check this is string type or not single Dimension are or not that's it so 

this is valid so this is valid but both are not exactly the same so jvm look for which main method 

the second one or first one jvm is looking for second one okay this is the method jvm look for 

okay this is another method just like a normal method right so remember these points so inter 

perspective these are all most important okay they will ask you these type of questions 

so by seeing them you should not be confused okay you should be more clear about this all 

right so this is all about static keyword so practice this for today's session and this 

keyword and static keyword and also we have tried we have understood what is system. 

out. pinell which we are using every day but we don't know exactly what is system. 

pinell so now we have understood also we have understood what is public static void mean 

what are these keywords and the next session we will start another objectoriented programming 

concept which is inh okay so meanwhile you can just practice this for today's session and 

tomorrow we will continue with other things

Need a transcript for another video?

Get free YouTube transcripts with timestamps, translation, and download options.

Transcript content is sourced from YouTube's auto-generated captions or AI transcription. All video content belongs to the original creators. Terms of Service · DMCA Contact

Session 13- Java OOPS Concepts - Usage of this and static...