Local Objects

Objects that are declared inside a method are called local objects. There are two kinds of local objects:

  1. Local variables are objects that have been declared inside the body of the method.

    They follow all the rules that we have already learned for objects. You can declare primitives or class objects. Make sure to use new and call your constructors for class objects.

  2. Local parameters are objects that are in the method's signature.

    You may have noticed that the argument list in a signature looks like declarations separated with commas. This is because they are declarations. When the method is called, the parameters are declared and initialized to the values passed as arguments in the method call.

    If the parameter is a class object (as opposed to a primitive), the local parameter is a reference to the object passed in. This means that if the object is changed in the method, it will keep the changes once the method returns. You should not use a local parameter as the lvalue in an assignment unless your method explicitly wants to change its value.

These objects are called "local", or "local to the method" because they only exist while the method is being executed.