Home > DeveloperSection > Articles > The static Keyword: Access Restrictions in Static Methods

The static Keyword: Access Restrictions in Static Methods


Java Java 
Ratings:
0 Comment(s)
 178  View(s)
Rate this:

The static Keyword: Access Restrictions in Static Methods


Because a static method is invoked without a class instance, we cannot use a this or super reference in the static method. That is to say, it is illegal to reference any of the class fields or methods using a this reference within a static method. However, a static method can access the class members (both fields and methods). A class member is a member of the class that is declared using the static keyword. As we have seen so far, both fields and methods can be declared with the static keyword. Such static fields and methods are accessible to the code defined in a static method. The non-static fields are bound to a class instance and therefore cannot be accessed in the body of a static method. To understand these code restrictions in a static method, look at the code snippet in here.

class StaticMemberTestApp {                  

                     private static int i;

                     private int j;

 

                     public static void staticMethod() {

                             // do something

                     }

 

                     public void nonStaticMethod() {

                             // do something else

                     }

 

                     public static void main(String[] args) {

                             i = 5;

                             j = 10; // this does not compile

                             staticMethod();

                             nonStaticMethod(); // this does not compile

                     }

}

 

The StaticMemberTestApp class declares two fields: The variable field i is declared static whereas the variable j is nonstatic. The main method, which itself is static, modifies the value of i, which is legal. However, accessing j in the body of the main method generates a compile-time error. Note that a static method cannot access a non-static attribute within its body. Similarly, the class declares two methods: one static and one non-static. The staticMethod is a class method (static) and is called within the body of the main method without producing any compile-time errors. However, calling the nonStaticMethod, which is an instance (nonstatic) method, within the body of the main method generates a compile-time error.

Another important code restriction is that a static method cannot be overridden in a subclass.

class MyClass {

                     public static void staticMethod() {

                             // do something

                     }

 

                     public void nonStaticMethod() {

                             // do something

                     }

}

 

class MySubClass extends MyClass {

                     public void staticMethod() // this does not compile

                     {

                             // overrides base class implementation

                     }

 

                     public void nonStaticMethod() {

                             // overrides base class implementation

                     }

}

 

The class MyClass declares two methods: one static and one nonstatic. The class MySubClass inherits MyClass. The class MySubClass now attempts to override the two methods inherited from its parent. Overriding the staticMethod, which is declared using the static qualifier in the base class, generates a compile-time error. Overriding a nonstatic method (that is, nonStaticMethod) is permitted.

Some Important Notes on Static Methods

Here are a few important points about static methods:

·       A static method is invoked via a class reference. You can use an object reference to invoke a static method; however, this is generally not considered good style.

·       The special method main is declared static so that we do not need to create an instance of the class to which it belongs while starting the application.

·         A static method cannot use this and super in its body.

·         A static method can access static fields and methods of the class.

·         A static method cannot access nonstatic fields and methods of the class.

·         A static method cannot be overridden in a subclass.


Don't want to miss updates? Please click the below button!

Follow MindStick