Ads Sponsored By Google :)

Sunday, 27 January 2013

why|what are the common errors while using static variable|method|context with example|we cannot refer non|static from


Common error while using static method why we cannot refer non static from static context common errors while using static  common error while using static  common errors while using static method common errors while using static variable what are the errors while using static method with example Common errors while using static method with example what are the errors while using static method why we cannot refer super and this from static context


 Common errors while we are using static method:

     If we are trying to refer any method or variable which are not static from the static method,  
   we will get compilation error.
   static() method  has some several restrictions:
   1. Can ONLY call another static ()
   2. MUST ONLY access static data
   3. cannot refer to super or this
   4. cannot refer non static from static context.
  
    The Following Example  will Clarifies Errors in static method and variables:

     public class StaticError {
      int s=10
     static void onemet(){
          //  s=100; INVALID because  cannot refer instance or non static 
         //members of class from static context
       System.out.println("this is one method");
          }

    static void testStaticMethod(){
          onemet();// Calling one static method from another static   //method.
               }
  
    public static void main(String args[]){
       testStaticMethod();
           }
      }

Explaination:

               The above program which illustrate that

          1)    s=100; INVALID because cannot refer instance or non static members of class from         
                static context. Because static members are called only once, but instance members    
               called multiple times using object
         2)   Calling one static method from another static method.
         3)   Cannot refer to super or this because they represents instance data members like 
               object, even Super() and this() functions are used calling constructors indirectly 
               creation of instances, so we cannot refer it.


No comments:

Post a Comment

LinkWithin