Wednesday, February 10, 2021

 what is static keyword in java ??

and how does it work ??


static keyword is used for many purpose like saving memory 

when you declared static as a variable methods ,blocks etc 

then it will allocate in a memory in ones time in a memory when your class will load 

it will directly stored in a stack memory 

basically memory are devided into two parts i.e Heap memory and Stack memory inside RAM memory.


when you make your methods,variables as a static member then you don't have need to make a object 

of a class you can simply access your method as a class name 

not need to make object of a class then access 


example; if you declare your method as a instance  member

then you will need to make object of a class then you can access 


see : public  Myclass {

...............

..............


mymethod(){

..............

...............

}

}


Myclass obj =new Myclass();

obj.mymethod     //you are accesing mymethod  with the help of obj1


if you declared as a static member 

then you haven't need to make object of a class

you simply access with the name of 

class

like that  ex:  Myclass.obj  //you are  accessing mymethod with the name of  your class

No comments:

Post a Comment

Featured Posts