What is the difference between a member variable and a local variable?

前端 未结 7 747
再見小時候
再見小時候 2020-11-29 08:01

What is the difference between a member variable and a local variable?

Are they the same?

相关标签:
7条回答
  • 2020-11-29 08:38

    A member variable belongs to an object... something which has state. A local variable just belongs to the symbol table of whatever scope you are in. However, they will be represented in memory much the same as the computer has no notion of a class... it just sees bits which represent instructions. Local variables and member variables can both be on the stack or heap.

    0 讨论(0)
  • 2020-11-29 08:44

    There are two kinds of member variable: instance and static.

    An instance variable lasts as long as the instance of the class. There will be one copy of it per instance.

    A static variable lasts as long as the class. There is one copy of it for the entire class.

    A local variable is declared in a method and only lasts until the method returns:

    public class Example {
        private int _instanceVariable = 1;
        private static int _staticvariable = 2;
    
        public void Method() {
            int localVariable = 3;
        }
    }
    
    // Somewhere else
    
    Example e = new Example();
    // e._instanceVariable will be 1
    // e._staticVariable will be 2
    // localVariable does not exist
    
    e.Method(); // While executing, localVariable exists
                // Afterwards, it's gone
    
    0 讨论(0)
  • 2020-11-29 08:45

    A local variable is the variable you declare in a function.Its lifespan is on that Function only.

    A member variable is the variable you declare in a class definition.Its lifespan is inside that class only.It is Global Variable.It can be access by any function inside that same class.

    0 讨论(0)
  • 2020-11-29 08:46

    A local variable is the variable you declare in a function.

    A member variable is the variable you declare in a class definiton.

    0 讨论(0)
  • 2020-11-29 08:49
    • Variables declared within a method are "local variables"
    • Variables declared within the class not within any methods are "member variables"(global variables).
    • Variables declared within the class not within any methods and defined as static are "class variables".
    0 讨论(0)
  • 2020-11-29 08:50

    A member variable is a member of a type and belongs to that type's state. A local variable is not a member of a type and represents local storage rather than the state of an instance of a given type.

    This is all very abstract, however. Here is a C# example:

    class Program
    {
        static void Main()
        {
            // This is a local variable. Its lifespan
            // is determined by lexical scope.
            Foo foo;
        }
    }
    
    class Foo
    {
        // This is a member variable - a new instance
        // of this variable will be created for each 
        // new instance of Foo.  The lifespan of this
        // variable is equal to the lifespan of "this"
        // instance of Foo.
        int bar;
    }
    
    0 讨论(0)
提交回复
热议问题