static keyword




            when you will want to define a class member that will be used independently of any object of that class.  However, it is possible to create a member that can be used by itself, without reference to a specific instance. To create such a member, precede its declaration with the keyword static. When a member is declared static, it can be accessed before any objects of its class are created, and without reference to any object. You can declare both methods and variables to be static. The most common example of a static member is main( )main( ) is declared as static because it must be called before any objects exist.Instance variables declared as static are, essentially, global variables. When objects of its class are declared, no copy of a static variable is made. Instead, all instances of the class share the same static variable.

Methods declared as static have several restrictions:
  • They can only call other static methods.
  • They must only access static data.
  • They cannot refer to this or super in any way. (The keyword super relates to 
  • inheritance.)
  • If Methods are declared as static ,they can access through the name of the class with out using class name
Example Programe:

public class ExStatic{
     public static void myStatic(){
          System.out.println("Static Method Executes with out using object");
     }
    public static void main(String a[]){
      ExStatic.myStatic();
    }
}

No comments:

Post a Comment