GeekInterview.com
  I am new, Sign me up!
 
GeekInterview.com  >  Interview Questions  >  J2EE
Go To First  |  Previous Question  |  Next Question 
 J2EE  |  Question 49 of 104    Print  
wat is local variable and instance variable?

  
Total Answers and Comments: 5 Last Update: August 16, 2006     Asked by: karthik 
  
 Sponsored Links

 
 Best Rated Answer
Submitted by: pankaj
 

There are three kinds of Java variables:

  1. Local variables are declared in a method, constructor, or block. When a method is entered, an area is pushed onto the call stack. This area contains slots for each local variable and parameter. When the method is called, the parameter slots are initialized to the parameter values. When the method exits, this area is popped off the stack and the memory becomes available for the next called method. Parameters are essentially local variables which are initialized from the actual parameters. Local variables are not visible outside the method.
  2. Instance variables are declared in a class, but outside a method. They are also called member or field variables. When an object is allocated in the heap, there is a slot in it for each instance variable value. Therefore an instance variable is created when an object is created and destroyed when the object is destroyed. Visible in all methods and constructors of the defining class, should generally be declared private, but may be given greater visibility.
  3. Class/static variables are declared with the static keyword in a class, but outside a method. There is only one copy per class, regardless of how many objects are created from it. They are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants.


Above answer was rated as good by the following members:
amit_9b
June 22, 2006 01:14:55   #1  
Amit Meshram        

RE: wat is local variable and instance variable?

Java has three kinds of variables: local (automatic) instance and class. Local variables are declared within a method body. Both instance and class variables are declared inside a class body but outside of any method bodies. Instance and class variables look similar except class variables are prepended with the static modifier.

class someClass {

// Instance Variable
visibility_modifier variable_type instanceVariableName;

// Class Variable
visibility_modifier static variable_type classVariableName;

returnType someMethod() {

// Local Variable
variable_type localVariableName;
}
}


 
Is this answer useful? Yes | No
June 26, 2006 11:48:51   #2  
wasimahmed Member Since: June 2006   Contribution: 1    

RE: wat is local variable and instance variable?
To make it more simpler i can say local / global variables are primitive type variables(like int/float etc) but you call a instance only for an object(i.e a variable declared of type user defined or built in class).
 
Is this answer useful? Yes | No
June 27, 2006 09:58:59   #3  
karthik        

RE: wat is local variable and instance variable?

Thanks for your explination.


 
Is this answer useful? Yes | No
August 01, 2006 08:12:32   #4  
santi Member Since: August 2006   Contribution: 3    

RE: wat is local variable and instance variable?

local variable is a variable declared inside a method whereas instance variable is declared at classlevel. if wrong plz advice


 
Is this answer useful? Yes | No
August 16, 2006 05:24:24   #5  
pankaj        

RE: wat is local variable and instance variable?

There are three kinds of Java variables:

  1. Local variables are declared in a method constructor or block. When a method is entered an area is pushed onto the call stack. This area contains slots for each local variable and parameter. When the method is called the parameter slots are initialized to the parameter values. When the method exits this area is popped off the stack and the memory becomes available for the next called method. Parameters are essentially local variables which are initialized from the actual parameters. Local variables are not visible outside the method.
  2. Instance variables are declared in a class but outside a method. They are also called member or field variables. When an object is allocated in the heap there is a slot in it for each instance variable value. Therefore an instance variable is created when an object is created and destroyed when the object is destroyed. Visible in all methods and constructors of the defining class should generally be declared private but may be given greater visibility.
  3. Class/static variables are declared with the static keyword in a class but outside a method. There is only one copy per class regardless of how many objects are created from it. They are stored in static memory. It is rare to use static variables other than declared final and used as either public or private constants.

 
Is this answer useful? Yes | NoAnswer is useful 1   Answer is not useful 1Overall Rating: -N/A-    


 
Go To Top


 Sponsored Links

 
About Us -  Privacy Policy -  Terms and Conditions -  Contact -  Ask Question -  Propose Category -  Site Updates 

Copyright © 2005 - 2009 GeekInterview.com. All Rights Reserved

Page copy protected against web site content infringement by Copyscape