Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials:
 

Software Solutions and Services
 

 
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments
 
Example of static method 
 

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

                         

» View all related tutorials
Related Tags: c file array class list ui lists method get name using this oo root example where to exam drive store

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 

Current Comments

4 comments so far (
post your own) View All Comments Latest 10 Comments:

Tq for the sample program. it's simple, but very clear.

Posted by may on Friday, 04.10.09 @ 20:05pm | #86716

it is realy very helpful to me.

Posted by kissan on Wednesday, 08.13.08 @ 16:35pm | #72637

Can a static class inherit non static class?

Posted by kaushal on Wednesday, 06.11.08 @ 13:03pm | #63004

can u give betterexample regarding static method

Posted by praveen on Wednesday, 02.14.07 @ 21:23pm | #7903

Training Courses
Tell A Friend
Your Friend Name
Website Designing Services
 
Web Designing Packages From $150!
 
Website Designing Company Web Hosting
 
Website Designing Quotation
 
Search Tutorials:

 

 
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Flex Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.