Home Java Beginners Access Static Member Of The Class Through Object

 
 

Share on Google+Share on Google+

Access Static Member Of The Class Through Object

Advertisement
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

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: June 27, 2008

Related Tutorials

Discuss: Access Static Member Of The Class Through Object  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com