Static Variables Methods and Blocks

           You may be familiar with the static keyword. Let us see what is static keyword and where it is used. First let us know about static variables and then followed by methods and blocks.
Static Variables: The variables whose values are unchanged until the end of the class are called Static Variables. These variables are also called as Class Variables. The memory for this variables is allocated before the object is created. These variables are accessed using the class name.  
Static Methods: These are the methods whose names are prefixed with static keyword. These are accessed by using the class name. The following is the syntax for calling a static method. 

  •  <class_name> <Method_name>;   
Static Blocks: These are the blocks executed before the object is created. The following is the syntax for defining static blocks.
  •          Syntax:      static {
                                         .....   
                                         ....    
                                      }          

When we compile the program JVM first looks for the static variables , then methods followed by blocks and allocates memory for them respectively.











No comments:

Post a Comment