0
1.4kviews
Explain Static Members

Mumbai University > Information Technology > Sem 3 > Object Oriented Programming Methodology

Marks: 10M

Year: Dec 2015

1 Answer
0
2views

Types of Static Members:

Java supports four types of static members

  1. Static Variable
  2. Static Blocks
  3. Static Methods
  4. Main Method (static method)
  • All static members are identified and get memory location at the time of class loading by default by JVM in Method area.
  • Only static variables get memory location, methods will not have separate memory location like variables.
  • Static Methods are just identified and can be accessed directly without object creation.

Static Variable:

  • A class level variable which has static keyword in its creation statement is called static variable.

    package com.instanceofjava;
    class Demo{
    
    static int a=10;
    
    static int b=20;
    
    }
    
  • We can not declare local variables as static it leads to compile time error "illegal start of expression".

  • Because being static variable it must get memory at the time of class loading, which is not possible to provide memory to local variable at the time of class loading.

    package com.instanceofjava;
    

    class Demo{

    static int a=10;

    static int b=20;

    public static void main(String [] args){

    //local variables should not be static

    static int a=10;// compile time error: illegal start of expression

    }

  • All static variables are executed by JVM in the order of they defined from top to bottom.

  • JVM provides individual memory location to each static variable in method area only once in a class life time. Life time and scope:
  • Static variable get life as soon as class is loaded into JVM and is available till class is removed from JVM or JVM is shutdown.
  • And its scope is class scope means it is accessible throughout the class.

    package com.instanceofjava;
    
    class StaticDemo{
    
    static int a=10;
    
    static int b=20;
    
    public static void main(String [] args){
    
         System.out.println("a="+a);
    
         System.out.println("a="+b);
    
    show();
    
    }
    
    public static void show(){
    
         System.out.println("a="+a);
    
         System.out.println("a="+b);
    
    }
    
    }
    

enter image description here

Duplicate Variables:

  • If multiple variables are created with same name are considered as duplicate variables.
  • In the same scope we can not create multiple variables with same name.
  • If we create it leads to compile time error "variable is already defined".
  • Even if we change data type or modifier or its assigned value we can not create another variable with same name.

    package com.instanceofjava;
    class StaticDemo
    {
        static int a=10;
         int a=30;// Compile time error 
    }
    
  • But it is possible to create multiple variables with same name in different scopes.

    package com.instanceofjava;
    class StaticDemo
    {
    static int a=10;
    int a=30;// Compile time error
    public static void main(String [] args){
    // it is allowed to define "a" in this method.
    int a=20;
    }
    
Please log in to add an answer.