What is meant by Local variable and Instance variable?

What is meant by Local variable and Instance variable?

Asked on January 18, 2019 in Java.
Add Comment


  • 4 Answer(s)

     Local variables are defined in the method and scope of the variables that have existed inside the method itself.

    An instance variable is defined inside the class and outside the method and scope of the variables exist throughout the class.

    Answered on January 18, 2019.
    Add Comment

    Local variables − Variables defined inside methods, constructors or blocks are called local variables. The variable will be declared and initialized within the method and the variable will be destroyed when the method has completed.

    Instance variables − Instance variables are variables within a class but outside any method. These variables are initialized when the class is instantiated. Instance variables can be accessed from inside any method, constructor or blocks of that particular class.

    Answered on January 25, 2019.
    Add Comment

    What is a Local Variable?

    local variable in Java is typically used in a method, constructor, or bloc and has only local scope. So, you can use the variable only within the scope of a block. Other methods in the class aren’t even aware that the variable exists.

    Example

    if(x > 100)

    {
    String testLocal = “some value”;
    }

    What is an Instance Variable?

    An instance variable is a variable that’s bound to the object itself. Instance variables are declared in a class , but outside a method. And every instance of that class (object) has it’s own copy of that variable. Changes made to the variable don’t reflect in other instances of that class. Instance variables are available to any method bound to an object instance . As a practical matter, this generally gives it scope within some instantiated class object. 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.

    Example

    class TestClass

    {
    public String StudentName;
    public int age;
    }

    Rules for Instance variable

    1. Instance variables can use any of the four access levels
    2. They can be marked final
    3. They can be marked transient
    4. They cannot be marked abstract
    5. They cannot be marked synchronized
    6. They cannot be marked native
    7. They cannot be marked static
    Answered on February 3, 2019.
    Add Comment

    Scope: Local variables are visible only in the method or block they are declared whereas instance variables can been seen by all methods in the class. Place where they are declared: Local variables are declared inside a method or a block whereas instance variables inside a class but outside a method.

    Answered on February 5, 2019.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.