Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
Java
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

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

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

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

Latest Searches:
datetimepicker and css
add the ArrayList to
Advance
spring applicationcont
JBOSS 4.0
Reading File into Byte
spring
a simple applet progra
Bluewall GNU/Linux 1.2
html table wrap
populate drop down
Creating Canvas Form E
setBounds
select a row in jsp
'string variable in sw
Servlet Example To Dis
header
data compression
calculate time
PHP Email Systems Crea
ejb architecture
session validation in
Two Dimensional Array
Real-World AJAX
FlowLayout
left align
how to pass same varia
Java example program t
Javascript Randomizing
JTree Open Icon
struts2 checkbox
HibernateTemplate
Statsmanager
flex
FileLock
partially selected che
addition of two number
errors in lbs
java is useful in netb
Java
api struts
io
html:button jsp exampl
code for how connect j
java form generator
substring
Convert Decimal into B
kaptcha
mysql insert statement
tree
jspf
code of image upload i
java object oriented
Time
spring hibernate ehcac
advantages of jsf
jQuery To Hide the Div
Photoshop Effects Pixe
sort object +compareTo
how to insert a data i
log4j
Print in particular po
Hash Function in Java
Visual Basic .NET Adju
break?â?¬â?????????â?¬
JTable size
STRUTS-VALIDATOR
Refresh a Web Page Usi
web crawling
INTRODUCTION TO Java s
dynamic table
sample codes of hibern
OutputStreamWriter
railway reservation sy
How to insert multiple
weblogic portal
jsf in struts
jsp MENU SELECT MAKES
Photoshop Photo Retouc
get url
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

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 | 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.