Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Differences between local variables and instance variables

  1. #1
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Differences between local variables and instance variables

    Hi I would really appreciate it if someone could briefly describe what the differences are between local variables and instance variables.
    Thank you.


  2. #2
    Member
    Join Date
    Jan 2012
    Location
    Hyderabad, Andhra Pradesh, India
    Posts
    32
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Differences between local variables and instance variables

    Consider the following code:
    class a
    {
    int a;
    void b()
    {
    int v;
    }}
    Here a is an instance variable. Every object created from this class will have its own copy of a. Instance variables are properties of an object. On the other hand, the variable v is a local variable that isn't a property of an object created from this class. Local variables are created whenever the method that declared them is invoked. Once control moves to the end of the method, local variables are destroyed.

    Now, consider the following test class
    class Test
    {
    public static void main(String args[])
    {
    a a1=new a1();
    a a2=new a2();
    a1.a=1;
    a2.a=2;
    System.out.println(a1.a+" "+a2.a); //the two objects a1 and a2 have their own values of a i.e a is the property of the instance of class a
    // This statement would give an error since v is a local variable and isn't a property of the object a1:   System.out.println(a2.v); 
    }
    }

  3. #3
    Junior Member
    Join Date
    Feb 2012
    Posts
    12
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Re: Differences between local variables and instance variables

    Thank you I really appreciate your comments.

Similar Threads

  1. Replies: 10
    Last Post: April 3rd, 2013, 03:16 PM
  2. Changing al instance variables at once?
    By Swen in forum Java Theory & Questions
    Replies: 1
    Last Post: December 30th, 2011, 07:42 AM
  3. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  4. [SOLVED] Instance data member vs Local variables (primitive/object reference)
    By chronoz13 in forum Java Theory & Questions
    Replies: 1
    Last Post: September 23rd, 2011, 12:42 AM
  5. Non-Static variables
    By liloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 31st, 2010, 09:13 AM