Results 1 to 3 of 3
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Are the terms "local variable" and "member variable" comparable?

    In my understanding,

    The term "Local variable" is related to scope. That is a local variable is one which is defined in a certain block of code, and its scope is confined inside that block of code.

    And a "Member variable" is simple an instance variable.

    I read in a discussion forum that when local variables are declared (example code below), their name reservation takes place in memory but they are not automatically initialized to anything. On the other hand, when member variables are declared, they are automatically initialized to null by default.

    Java Code:
    public void myFunction () {
        int [] myInt; // A local, member variable (because "static" keyword is not there) declared
    }
    So it seems that they are comparing local variables and member variables. While I think a member variable can also be be local in a block of code, isn't it?

    Are they mistaking the term "local" for "static"?

  2. #2
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Are the terms "local variable" and "member variable" comparable?

    Member variables are not local. They are instance variables (or fields) as you first stated. And member variables are only initialized
    to null if they are objects. Numbers are initialized to 0 and booleans to false. Static variables are class variables, neither local nor member.

    Also, you can't declare a local variable to be static.

    Regards,
    Jim
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  3. #3
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    6

    Default Re: Are the terms "local variable" and "member variable" comparable?

    @Jim829 Thank you very much

Similar Threads

  1. Replies: 3
    Last Post: 06-26-2014, 06:54 AM
  2. Help needed with "Variable is not public in component" error.
    By Humphrey Bogart in forum New To Java
    Replies: 3
    Last Post: 05-29-2013, 09:27 PM
  3. Replies: 0
    Last Post: 12-07-2012, 08:29 AM
  4. Replies: 3
    Last Post: 10-30-2012, 03:06 PM
  5. Replies: 1
    Last Post: 10-20-2008, 07:35 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •