Well yes and no!
It depends
alot on how the methods method1 and method2 are defined.
It may so happen that method1 and method2 touch a static variable in a non-thread-safe way.(as you said UnsafeClass is not threadsafe)
In such a case even though unsafeClass is a local variable, the invocation of method1 and method2 may wreck havoc.
Also, if i change your code to:
then, the local variable unsafeClass "escapes" from the method and can be used in a non-thread-safe way.
What i am trying to say is that, making blanket statements like
is thread safe, even though UnsafeClass is not thread safe, because UnsafeClass is now part of the thread's 'safe' stack?
have to made with care and *(conditions apply
)
It is correct that local variables are on the method stack which is not shared among threads but the objects are always created on the heap.
So, if it may so happens that someone can get hold of this object (eg: if the reference escapes from the method) then no longer is the thread safety guaranteed.