Wat is local variable and instance variable?

Showing Answers 1 - 5 of 5 Answers

Amit Meshram

  • Jun 22nd, 2006
 

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;
      }
   }

  Was this answer useful?  Yes

wasimahmed

  • Jun 26th, 2006
 

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

  Was this answer useful?  Yes

karthik

  • Jun 27th, 2006
 

Thanks for your explination.

  Was this answer useful?  Yes

santi

  • Aug 1st, 2006
 

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

  Was this answer useful?  Yes

pankaj

  • Aug 16th, 2006
 

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.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

 

Related Answered Questions

 

Related Open Questions