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 instance variable

                         

When a number of objects are created from the same class,  the same copy of  instance variable is provided to all. Remember, each time you call the instance the same old value is provided to you, not the updated one . In this program we are showing that how a instance variable is called in each instance, but the copy remains same irrespective the counter we have used in the constructor of a class. We can't call the non-static variable in a main method. If we try to call the non-static method in the main method then the compiler will prompt you that non-static variable cannot be referenced from a static context. We can call the non-static variable by the instance of the class.

The example will show you how you can use a non-static variables. First create a class named NonStaticVariable. Declare one global variable and call it in the constructor overloaded method in which you will have to increment the value of the variable by the counter. To access a  non-static variable we will have to make a object of NonStaticVariable by using new operator. Now call the instance variable. Now again make a new object of the class and again call the instance variable. Now you can realize that the value of the instance variable in both the object is same. 

Code of this program is given below:

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

    NonStaticVariable st2 = new NonStaticVariable();
    System.out.println("No. of instances  for st1  : " + st1.noOfInstances);
    System.out.println("No. of instances  for st2  : " + st2.noOfInstances);

    NonStaticVariable st3 = new NonStaticVariable();
    System.out.println("No. of instances  for st1  : " + st1.noOfInstances);
    System.out.println("No. of instances  for st2  : " + st2.noOfInstances);
    System.out.println("No. of instances  for st3  : " + st3.noOfInstances);

      }
}

The output of this variable is given below :

As we can see in the output the same copy of the instance variable is provided to all the objects, no matter how many objects we create.

C:\java>java NonStaticVariable
No. of instances for st1 : 1
No. of instances for st1 : 1
No. of instances for st2 : 1
No. of instances for st1 : 1
No. of instances for st2 : 1
No. of instances for st3 : 1

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 
Latest Searches:
count row in resultset
tattoo efectes
Thread Sleep Method
export database data i
Date Examples java
The eXo platform
linked list and GUI
toInt()
primitive data types
abstract class and abs
jsp login by insertin
jdbcrealm
jQuery To Hide the Div
line break in javascri
div tag
projections
ajax
retrieve image url fro
JSON rest
what is basic stack in
infix to pasfix into s
my clips
jdbc mysql connection
Photoshop Drawing Awes
apache derby embedded
two dimensional array
calculator square root
J2se Tutorial
jquery using .net 2.0
jsp mysql login
java Concurrency utili
string Compareing
word
DOJO Container
A
how to subtract calend
objective type questio
Get Array Size
get url
struts.jar download
Tomcat deploy JSP
typecast between strin
how to reate a textfie
connect to sql databas
login session
javascript separate in
informatica
sorting program
adding label and textb
java constants
prime numbers
displaying text in a d
wireless c
give the input text fi
iterator
transposition
menu bar in java
PHP Email Systems Send
jsf selectitems
Photoshop Basics Add F
state management
excel from java
struts2.0 introduction
Photoshop Cut out imag
jstl list
Flag
codebar
Photoshop Photo Effect
java session
input in java
button onclick go to a
Spring ORM Module
ASP.NET .NET Custom Va
JtextArea enable
pdf box get text
Quartz Tutorial
Combo Box Using Ajax I
JDBC with Mysql in Ecl
PHP Site Navigation Ad
jasper report
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.