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 a class variable (static variable)

                         

This Java programming example will teach you how you can define the static class variable in a class. When a number of objects are created from the same class, each instance has its own copy of class variables. But this is not the case when it is declared as static static.

static  method or a variable is not attached to a particular object, but rather to the class as a whole. They are allocated when the class is loaded. Remember, each time you call the instance the new value of the variable is provided to you. For example in the class StaticVariable each instance has different copy of a class variable. It will be updated each time the instance has been called. We can call class variable directly inside the main method.

To see the use of a static variable first of all create a class StaticVariable. Define one static variable in the class. Now make a  constructor  in which you will increment the value of the static variable. Now make a object of StaticVariable class and call the static variable of the class. In the same way now make a second object of the class and again repeats the process. Each time you call the static variable you will get a new value.

Code of this example is given below:

public class StaticVariable{
  static int noOfInstances;
  StaticVariable(){
    noOfInstances++;
  }
  public static void main(String[] args){
    StaticVariable sv1 = new StaticVariable();
    System.out.println("No. of instances for sv1 : " + sv1.noOfInstances);

    StaticVariable sv2 = new StaticVariable();
    System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);
    System.out.println("No. of instances for st2 : "  + sv2.noOfInstances);

    StaticVariable sv3 = new StaticVariable();
    System.out.println("No. of instances for sv1 : "  + sv1.noOfInstances);
    System.out.println("No. of instances for sv2 : "  + sv2.noOfInstances);
    System.out.println("No. of instances for sv3 : "  + sv3.noOfInstances);

           }
}

Output of the program is given below:

As we can see in this example each object has its own copy of class variable.

C:\java>java StaticVariable
No. of instances for sv1 : 1
No. of instances for sv1 : 2
No. of instances for st2 :  2
No. of instances for sv1 : 3
No. of instances for sv2 : 3
No. of instances for sv3 : 3

Download this example:

                         

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 
Latest Searches:
upload file using ajax
placeholder
Flex TreeGrid
Photoshop Drawing Penc
Quartz
Java / multi thread
ASP Ad Management Crea
insert data to text fi
example
create a tree to be di
WHERE Statement in SQL
factory method
username and password
Photoshop Animation An
java jtable
permutation
JDBC with Mysql in Ecl
multiple submit button
Button
data ware
oracle.jdbc.driver.Dat
server side validation
draw Polyline
web.xml file
convert string to stin
breakั?�??????ั?
org.gjt.mm.mysql.Drive
compare java.util.date
struts formfile
JavaWorld article abou
Photoshop Basics Using
logout session
system.out.println(x
data binding
program to find greate
project in session bea
ant script web and ejb
select from statment
writing data to file
panelgrid colspan
jdbc connections
intranet applications
Exercise - Pad Left
example for org.jfree.
Delete database
XML Class Constructor
Passing Parameters in
Display detailed cloc
Reading Nested dom ele
swt
page refresh
Add a file finder
Refresh a Web Page Usi
Build scripts with Gro
DOM
hibernate findall
insert fields blob
count row in resultset
token string
java programs usin pus
mysql blob
Insert an array list o
javabean program
Merge Sort In Javascri
write a program to tes
java applets
x509
connect a program writ
extract
Create XML file from f
lucene
Two Dimensional Array
file input output
HSSF clone cell
interview question
JSF 2
FileInputStream
hibernate many to many
source code for chat p
set()mutator method
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.