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

Thread: clarify the doubt about accesing instance variable and local variable

  1. #1
    Junior Member
    Join Date
    Nov 2011
    Posts
    7
    My Mood
    Busy
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default clarify the doubt about accesing instance variable and local variable

    if we use instance variable(without initialization or assigning any value) compiler won't give any error,but in case of local variable compiler gives error,

    i know that instance variables initializes with default values but local variables won't..

    why it is so...


  2. #2
    Think of me.... Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Pakistan
    Posts
    1,136
    My Mood
    Grumpy
    Thanks
    20
    Thanked 82 Times in 78 Posts
    Blog Entries
    1

    Default Re: clarify the doubt about accesing instance variable and local variable

    How do you define local variables?
    Is this;
    int a;

    ??? If yes, then it's not local variable but primitive type or compile time type variable. And it's auto initialized by it's default value, that is 0

  3. #3
    Member
    Join Date
    Nov 2011
    Posts
    37
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: clarify the doubt about accesing instance variable and local variable

    Hi,
    Can you show your code.

    Core java
    Last edited by mr.miku; January 11th, 2012 at 06:14 PM.

  4. #4
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: clarify the doubt about accesing instance variable and local variable

    Quote Originally Posted by Mr.777 View Post
    How do you define local variables?
    Is this;
    int a;

    ??? If yes, then it's not local variable but primitive type or compile time type variable. And it's auto initialized by it's default value, that is 0
    That's not actually true. Local variables (variables inside methods) do not get automatically initialized like class variables do. That's the source of the "variable may not have been initialized" compiler error. And I'm not sure what you mean by saying that it's not a local variable but a primitive type- local variables can hold primitive types, but they certainly don't have to. Not sure what you mean at all.

    And to the OP- Your question is why is this the case? The short answer is because that's how the designers of Java made it. You can argue for or against it all you like, but it's simply true. And any argument for or against it is mostly guesswork anyway. Best just to accept it and move on.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  5. #5
    Crazy Cat Lady KevinWorkman's Avatar
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    5,447
    My Mood
    Hungover
    Thanks
    144
    Thanked 636 Times in 544 Posts

    Default Re: clarify the doubt about accesing instance variable and local variable

    Quote Originally Posted by mr.miku View Post
    Hi,
    Can you show your code.
    What code? He's asking about a general, obvious concept. I'm all for showing code, but in this case, it's not necessary.
    Useful links: How to Ask Questions the Smart Way | Use Code Tags | Java Tutorials
    Static Void Games - Play indie games, learn from game tutorials and source code, upload your own games!

  6. #6
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: clarify the doubt about accesing instance variable and local variable

    It is called Definite Assignment. Read the following links (especially the motivation section in the second link)
    Definite Assignment
    Definite assignment analysis - Wikipedia, the free encyclopedia

  7. The Following User Says Thank You to copeg For This Useful Post:

    KevinWorkman (November 16th, 2011)

Similar Threads

  1. Instance Variables and local variables difference
    By dcwang3 in forum Java Theory & Questions
    Replies: 3
    Last Post: October 31st, 2011, 06:33 AM
  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. [SOLVED] what is this? Variable?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: May 7th, 2011, 11:32 PM
  4. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 02:24 AM
  5. Reset the value of each variable
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 22nd, 2009, 09:45 AM