Skip to main content

Static Modifier

                                                Static Modifier

static is a modifier applicable for methods and variables but not for classes. we can't declare top-level classes with static modifiers but we can declare inner classes as static (such type of inner classes are called static nested classes).

  • in the case of an instance variable for every object separate copy will be created but in the case of a static variable, a single copy will be created at class level and shared by every object of that class.

        eg-:

          class Test{
                static int x=10;
                int y=20;
                public static void main(String[] args){
                    Test t=new Test();
                    t.x=222;
                    t.y=999;
                    Test t1=new Test();
                    System.out.print(t1.x+"     "+t1.y);
              }
        }
       //output is-: 222     20

  • we can't access instance members directly from the static area but we can access from the instance area directly.
          eg-:
          class Test{
                int x=10;
                public void m(){
                    System.out.print(x);// we can access directly because this is instance area
                }
           public static void main(String[] args){
              System.out.print(x);    //we cannot access it directly because this is a static area.
           }
     }
  • we can access static members from both static and instance are access directly.
      eg-:
        class Test{
               static int x=10;
                public void m(){
                    System.out.print(x);
                }
           public static void main(String[] args){
              System.out.print(x);   
           }
     }
consider the following declaration.
  1. int x=10;
  2. static int x=10;
  3. public void m(){
       System.out.print(x);
    }
  4. public static void m(){
        System.out.print(x);
    }
      options-:

      (a)  . 1 & 3    True
      (b)  . 1 & 4    compile time error-: non static variable x cannot be referenced static context
      (c)  .  2 & 3    True
      (d)  .  2 & 4    True
      (e)   .  1  & 2    compile time error-: variable x is already define in Test
      (f)   .  3 & 3     compile time error -: m() is already define in Test

within the same class which of the above declarations, we can take simultaneously.

case 1-: overloading concept applicable for static method including main() but JVM always called  main(String[] agrs) only.
other overloaded methods we have to call just like a normal method class.
eg-:

class Test{
               public static void main(int[] args){
                    System.out.print("int[]");
                }
           public static void main(String[] args){
              System.out.print("String[]");
           }
     }
    // output-: String[]

case 2-: inheritance concept applicable for static method including main() hence while executing child class if child doesn't contain main() then parent class main() will be executed.

eg-:

class Test{
        public static void main(String[] args){
                System.out.print("parent method");
         }
    }
class P extends Test{
}

java Test -> press enter
o/p-: parent method
java P
o/p -: parent method

case 3-:it seems overriding applicable for the static method but it is not overriding and it is method hiding.
Note-:for static methods overloading and inheritance concepts of applicable but the overriding concepts are not applicable .but instead of overriding method hiding concepts are applicable.

class Test{
        public static void main(String[] args){
                System.out.print("parent method");
         }
    }
class P extends Test{
      public static void main(String[] args){
          System.out.print("child method");
      }
}
java Test -> press enter
o/p-: parent method
java P
o/p -: child method

case 4-:Inside method implementation, if we are using at least one instance variable then that method talks about a particular object hence we should declare a method as an instance method.

Inside method implementation, if we are not using any instance variable then this method no way related to a particular object hence we have to declare a method as a static method. irrespective of whether we are using static variables or not.

eg-:
class Test{
   int rollNo;
   int marks;
   String name;
   static String collageName;
   getStrudentInfo(){
      return name+"......."+marks;  //Instance method
   }
   getStudentCollageName(){
       return collageName; //Static method
   }
   getAverage(int x,int y){
      return (x+y)/2;    //utility method
  }
  getCompleteInfo(){
     return rollNo+".........."+marks+".........."+name+"........"+collageName; //Instance Method
  }
}
case 5-:for the static method, the implementation should be available whereas for the abstract method implementation is not available hence abstract static combination is illegal for the method.

Popular posts from this blog

Java

Codes With Java — Basics Codes With Java Java tutorials & fundamentals About Contact Privacy Basic Fundamentals Java source file structure Import Statement Static Import Packages Data Type Variables Final Variable Declaration and Access Modifier Inner classes applicable modifiers Static Modifier Synchronized Native Transient Volatile Interface Introduction Interface Declaration and Implementation Interface methods and variables Naming Conflicts Interface Marker interface and Ad...

Short Circuite Operators part 4

                                                             Short Circuit Operators In  Java logical operators , if the evaluation of a logical expression exits in between before complete evaluation, then it is known as  Short-circuit . A short circuit happens because the result is clear even before the complete evaluation of the expression, and the result is returned. Short circuit evaluation avoids unnecessary work and leads to efficient processing. 1-: AND(&&) 2-:OR(||) these are exactly same as bitwise operators (&,|) except the following differences. Single Short Circuit Operator(&,|) Both arguments Should be evaluated always. relatively performance is low. Applicable for both boolean and Integral types. Double Short Circuit Operator(...

Final Variable

Final Instance Variable If the value of a variable changes from object to object, such a variable is called an instance variable . For every object, a separate copy of the instance variable will be created. Instance variables do not require explicit initialization; JVM always provides default values. Example: class Test { int x; // instance variable public static void main(String... args) { Test t = new Test(); System.out.println(t.x); // output: 0 } } If the instance variable is declared as final , then explicit initialization is mandatory. JVM will NOT provide default values. Example: class Test { final int x; } Compile-time Error: variable x might not have been initialized. Rule: A final instance variable must be initialized before constructor completion . Possible places for initialization: 1. At the time of declaration class Test { final int x = 10; } 2. Inside an instance blo...