Home Java Beginners Access Static Member Of The Class Through Object



Access Static Member Of The Class Through Object
Posted on: June 27, 2008 at 12:00 AM
Static methods are special type of methods that work without any object of the class.

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

Related Tags for Access Static Member Of The Class Through Object:
cclassvariablesstaticobjectmethodsmethodvariabletypelimitnameusingriashowexamplecallwithworktoexamciwsshboteitcallinglimitvarpeimceincalasstamoutcajclesspecialspecthroughwithoutallmehowhrobjxaxampsspessatanyplykhallmplfollowandarstatccsimwingvassrithshostaccessabablatihatjepleplmindonlyodsonolonl


More Tutorials from this section

Ask Questions?    Discuss: Access Static Member Of The Class Through Object  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

Ask Questions?

If you are facing any programming issue, such as compilation errors or not able to find the code you are looking for.

Ask your questions, our development team will try to give answers to your questions.