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 4 of 4

Thread: Instance Variables and local variables difference

  1. #1
    Junior Member
    Join Date
    Oct 2011
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Instance Variables and local variables difference

    I have a question about instance variables and local variables...

    When you have an instance variable defined outside of every method but inside a class (say variable named "X"), and you create a local variable the same name ("X") within a certain method, will the instance variable get changed? There are two examples below.

    private int X;
     
    private void test(int y)
    {
         int X;
     
         X = y;
    }

    Will the variable X will be changed within the method, but will that change the instance variable X?

    private int X
     
    private void test(int X)
    {
         X = X + 1;
    }

    How about the example above? Does the local variable take precedence over the global variable in those cases?


  2. #2
    Grand Poobah
    Join Date
    Mar 2011
    Posts
    1,545
    My Mood
    Grumpy
    Thanks
    0
    Thanked 167 Times in 158 Posts

    Default Re: Instance Variables and local variables difference

    You have written the code. Why don't you compile and run it to see what happens?
    Improving the world one idiot at a time!

  3. #3
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: Instance Variables and local variables difference

    See: Java Gotchas: Instance Variables Hiding | JAVA Developer's Journal.

    For more information, do a search on Java variable hiding.

  4. #4
    Member
    Join Date
    Oct 2011
    Posts
    50
    My Mood
    Fine
    Thanks
    2
    Thanked 1 Time in 1 Post

    Default Re: Instance Variables and local variables difference

    The second code snippet won't compile .

    Now, to refer the outer variable, you might use this.X. That should do the work, otherwise it's refered only locally.

    application context
    Last edited by daniel.j2ee; December 13th, 2011 at 04:57 PM.

Similar Threads

  1. Replies: 10
    Last Post: April 3rd, 2013, 03:16 PM
  2. [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
  3. Non-Static variables
    By liloka in forum What's Wrong With My Code?
    Replies: 3
    Last Post: December 31st, 2010, 09:13 AM
  4. Using variables from different methods?
    By Morevan in forum Object Oriented Programming
    Replies: 3
    Last Post: January 5th, 2010, 07:10 PM
  5. [SOLVED] Difference between public and private variable and their uses
    By napenthia in forum Java Theory & Questions
    Replies: 1
    Last Post: April 22nd, 2009, 11:36 AM