[an error occurred while processing this directive]

Access Static Member Of The Class Through Object

                         

Static methods are special type of methods that work without any object of the class. Static methods are limited to calling other static methods in the class and to using only static variables. The following example shows both ways to access static method (through simply class name and through object).

StaticMemberClass.java

                /**
               *
Access static member of the class through object.
               */
               import java.io.*;

               class StaticMemberClass {
                     
// Declare a static method.
                      public static void staticDisplay() {
                               System.out.println("This is static method.");
                     }
                    
// Declare a non static method.
                     public void nonStaticDisplay() {
                             System.out.println("This is non static method.");
                     }
                }

               class StaticAccessByObject {

                     public static void main(String[] args) throws IOException {
                             
// call a static member only by class name.
                             StaticMemberClass.staticDisplay();
                            
// Create object of StaticMemberClass class.
                             StaticMemberClass obj = new StaticMemberClass();
                            
// call a static member only by object.
                             obj.staticDisplay();
                            
// accessing non static method through object.
                             obj.nonStaticDisplay();
                    }
               }
     Output of the program:
    This is static method.
    This is static method.
    This is non static method.

Download source code

                         

[an error occurred while processing this directive]