Home Java Beginners Example of static method
Questions:Ask|Latest

 
 

Share on Google+Share on Google+

Example of static method

Advertisement
In this section, you will learn how to define a static method.

Example of static method

     

This Java programming example will teach you the way to define a static methods. In java we have two types of methods, instance methods and static methods. Static methods can't use any instance variables. The this keyword can't be used in a static methods. You can find it difficult to understand when to use a static method and when not to use. If you have a better understanding of the instance methods and static methods then you can know where to use instance method and static method. 

A static method can be accessed without creating an instance of the class. If you try to use a non-static method and variable defined in this class then the compiler will say that non-static variable or method cannot be referenced from a static context. Static method can call only other static methods and  static variables defined in the class. 

The concept of static method will get more clear after this program. First of all create a class HowToAccessStaticMethod. Now define two variables in it, one is instance variable and other is class variable. Make one static method named staticMethod() and second named as nonStaticMethod(). Now try to call both the method without constructing a object of the class. You will find that only static method can be called this way. 

The code of the program is given below:

public class HowToAccessStaticMethod{
  int i;
  static int j;
  public static void staticMethod(){
  System.out.println("you can access a static method this way");
  }
  public void nonStaticMethod(){
  i=100;
  j=1000;
  System.out.println("Don't try to access a non static method");
  }
  public static void main(String[] args) {
  //i=100;
    
   j=
1000;
  //nonStaticMethod();
  staticMethod();
  }
}

Output of the program is given below:

C:\java>java HowToAccessStaticMethod
you can access a static method this way

Download this program

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 8, 2007

Related Tutorials

Ask Questions?    Discuss: Example of static method   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments
vishnuvardhanreddy
April 17, 2011
java

super class static method is over writing in the sub class is possible or not?explain...
sachin mishra
June 17, 2011
static method

thanks!!! it's very clear and easy defination to learn.
Manoharan
July 10, 2011
Clear Answer

i dont underStand Static. i want clear ans...
Anuradha Kariyawasam
February 7, 2013
java

good
shekhar
December 4, 2011
java language

why use static keeyword in java
Nagaraju Kaveti
March 4, 2012
Static

simple explanation is very will,good thinking. thanks.
santosh
April 30, 2012
static and instance

what happens when a static method calls a static method with respect to object name if those two static methods are in same class..? what happens when a non-static method calls a static method directly(if those two static and non-static methods are in same class..)..?
Bhupesh Sharma
June 15, 2012
StaticMethod

i don't see any use of these variables : i=100; j=1000; it's just that you have to declare a variable static if you want to use that variable in the static method. it might be confusing if you see the code of this particular Example.
dhananjay singh shrinet
July 10, 2012
comments

nicely explained.....
Satish
July 12, 2012
Pls Explin this programme Out put.!!!!!!

class Csm { public static void doIt() { System.out.println("Am IN doIt();"); } } class StaticMethodDemo { public static void main(String ar[]) { Csm obj1=new Csm(); obj1.doIt(); Csm.doIt(); obj1=null; obj1.doIt(); } } /*OUT PUT: D:\java>javac StaticMethodDemo.java D:\java>java StaticMethodDemo aM IN doIt(); aM IN doIt(); aM IN doIt(); */
vinay kumar sharma
August 22, 2012
Static method and variable...

static { int x=90; } then what is the type of x variable..is it of type instance variable or class vaiable or any other variable..
mircl
September 7, 2012
thanks

Thank you :=)
chinna
September 22, 2012
java

why static variables are not accessed nonstatic
ali
August 19, 2013
hi

why use static keyword in java
DMCA.com