GeekInterview.com
   Home |  Tech FAQ  |   Interview Questions |  Placement Papers |  Tech Articles |  Learn |  Freelance Projects |  Online Testing |  Geeks Talk |  Job Postings |  Knowledge Base | Site Search |  Add/Ask Question

GeekInterview.com  >  Tech FAQs  >  Programming  >  Java
Go To First  |  Previous Question  |  Next Question 
 Java  |  Question 860 of 885    Print  
Class and Instance Variable
What is the difference between a class variable and an instance variable?


  
Total Answers and Comments: 1 Last Update: March 24, 2008     Asked by: chithira_s 
  
 Sponsored Links

 
 Best Rated Answer

No best answer available. Please pick the good answer available or submit your answer.
March 24, 2008 01:58:54   #1  
ravi_1229 Member Since: November 2006   Contribution: 7    

RE: Class and Instance Variable
Instance variables created  per copy.
for ex:
class A{
    private String instVariable="Hello";
    public static void main(String a[]){
       A obj1=new A();
       A obj2=new A();

    }
}

1) the above class creates two objects (obj1, obj2) for the class A
2) for each object there will be a copy of instVariable cerated in memory.
3) that means for obj1 there will be one instVariable and for obj2 there will be another instVariable.
4) i.e.,  obj1.instVariable!=obj2.instVariable
5) instance Variables are used by creating an object to that.


on the other hand,

Class Variables are also referred to static variables. Class variables  are created only once.  they can be called directly using  class name. they must be defined using static keyword.
ex:
class A{
 private staic String str="Hello World!!";
}
when we declare like this in a class the str object is instantiated and stored  in memory.
no matter how many objects you create for the
class A.
only one class variable(str) will be created in the memory and all the objects refer to that memory location.
    If you want to know how many times the class is being called , put a static variable and increment the variable in the constructor of that class.
 

for further information see :
http:// java.sun . com/docs/books/tutorial/java/javaOO/classvars . html

 
Is this answer useful? Yes | No

 Related Questions

As of now ,what i know abt cloning is that we can make a field to field copy of an object.ie when we clone an object,its state as well as a its behaviour is copied to the cloned object.My question is what is the neccesity of it? cant we create another object of the same class? so how does its different from cloned object (that is creating another object of that class rather than  being cloned).is there any security issues coupled with this cloned object? And What is the Concrete purpose of

Latest Answer : No it won't work.Only one main methods is allowed ...

Latest Answer :  Interface is a pure(100 %) abstract class. Then why abstract class seperatly? It's the design criteria,For example we are writing one class that has methods implemented which are common to other classes .And some methods are different for ...
Tags : Abstract

Latest Answer : In singleton class ,only one time the instance wil create.Example....public class Sin{  static Sin s=null;    public static void main(String a[])  {      create();     create();     ...

Latest Answer : padmareddy Wrote: can we acces static vairable with object                 we can access static variable by using objects also for example see the below program u will ...

Latest Answer : Below are three signatures:public final void notify()public final void notifyAll()public final void wait(long timeout) ...

Latest Answer : class is a blue print for an object which defines the functionality of an object. class also called the template for an object. ...

Latest Answer : another informative thing is;there are 8 wrapper classes in J2EE ...

Latest Answer : Please Check with this example programpublic class StaticExam { static int a=10;public static void main(String args[]){System.out.println(" Welcome to static variable testing");StaticExam s= new StaticExam();s.display();}public void display(){System.out.println(" ...

Latest Answer : abstract class nnothing but a class which can declear a data but in abclass the data canot shown it internaly working. ...
Tags : Abstract


 Sponsored Links

 
Related Articles

Service Oriented Java Business Integration Review

Service Oriented Java Business Integration Review Introduction If you ve read through the texts which give you an introduction to SOA or Web Services you will often find them to be quite frustrating and the reason for this is because they spend too much time referencing business processes which are
 

Concepts of Object-Oriented Programming

Object Oriented JavaScript In this chapter you ll learn about OOP Object Oriented Programming and how it relates to JavaScript As an ASP NET developer you probably have some experience working with objects and you may even be familiar with concepts such as inheritance However unless you re already a
 

C++ Pure Virtual Function and Base Class

C Pure Virtual Function and Virtual Base Class In this C tutorial you will learn about pure virtual function declaration of pure virtual function and virtual base class virtual base class and how to implement a virtual base class explained with examples mosgoogle center What is Pure Virtual Function
 

Java and Client Server Models

Java and Client Server Models The Role of Client Servers on the Web Client server models provide the essential mechanisms for working with the Internet In fact most of the World Wide Web is built according to this paradigm In client server models the web browsers run by millions of users are the cli
 

How to Access C++ Class Members

How to Access C Class Members In this C tutorial you will learn how to access Class members dot operator or class member access operator difference between struct and class and  scope resolution operator mosgoogle center It is possible to access the class members after a class is defined an
 

What happens when a variable is not initialized in main function?

When a variable is not initialized in main function it contains garbage value. This can be well seen from the example below main() { int x; printf(“%d”,x); z= sample() } sample() { printf(“Testing program”); } Output is   &n
 

What happens when a variable is not declared in function definition?

Generally in C program the function definition and calling takes the form as given below: main() { int x,y,z; z=sample(x,y); printf(“%d”,z); } sample(x1,y1) int x1,y1; { int z1; z1= x1 - y1; return(z1); } Here what happens is the values x, y gets passed to x1,y1
 

SQL Programming

SQL Programming Overview Anybody who has done something for a long time has probably wanted to change how things work at some point or another. A worker at a mill might have found a more efficient way of cutting logs, or a mathematics teacher might have had a hand in changing a school’s al
 

WinRunner Programming Concepts

If you want to create WinRunner scripts that are highly efficient, there are important programming concepts that you will want to become familiar with. Understanding these concepts will provide you with a large number of key benefits. In addition to understanding these concepts, you must also learn
 

Programming Languages Certification

IT Certification programs have several options that will offer you the best knowledge.  By learning everything that you need to know about information technology you will be able to open new doors to your career and personal business desires.  IT Certification offers several vari
 





About Us  |   Privacy Policy  |   Terms and Conditions  |   Contact  |   Site Map  |   Add Question  |   Propose Category  |   RSS Feeds  |   Articles Sitemap  |   Site Updates  |   Add Resource

Copyright © 2005 - 2008 GeekInterview.com. All Rights Reserved
Page copy protected against web site content infringement by Copyscape