Question: Is there a reason to prefer using shared instance variable in class vs. local variable and have methods return the instance to it? Or is either one a bad practice?

```
import package.AClass;

public class foo {
    private AClass aVar = new AClass();

    // ... Constructor

    public AClass returnAClassSetted() {
     doStuff(aVar);

     return avar;
    }

    private void doStuff(AClass a) {
     aVar = a.setSomething("");
    }
}

```

vs. 

```
import package.AClass;

public class foo {

    // ... Constructor

    public AClass returnAClassSetted() {
     AClass aVar = new AClass();
     aVar = doStuff();

     return aVar;
    }

    private AClass doStuff() {
     AClass aVar1 = new AClass();
     aVar1.setSomething("");

     return aVar1;
    }
}

```

First one makes more sense to me in so many ways but I often see code that does the second. Thanks!

Answer: 
Your class name should have been `Foo`.

The two versions you have are not the same, and it should depend on your use case. 

The first version returns the same `AClass` object when different callers call `returnAClassSetted()` method using the same `Foo` object. If one of them changes the state of the returned `AClass` object, all of them will get see the change. Your `Foo` class is effectively a [Singleton](https://en.wikipedia.org/wiki/Singleton_pattern). 

The second version returns a new `AClass` object every time a caller calls `returnAClassSetted()` method using either the same or different `Foo` object. Your `Foo` class is effectively a [Builder](https://en.wikipedia.org/wiki/Builder_pattern).

Also, if you want the second version, remove the `AClass aVar = new AClass();` and just use `AClass aVar = doStuff();`. Because you are throwing away the first `AClass` object created by `new AClass();`
In Instance variables values given are default values means null so if it's an object reference, 0 if it's and int.

Local variables usually don't get default values, and therefore need to be explicitly initialized and the compiler generates an error if you fail to do so.

Further,
 Local variables are only visible in the method or block in which they are declared whereas the instance variable can be seen by all methods in the class.