So I have junit tests, let's pretend this is my code:

public void test1() {
  Object o = new Object("123");
  AssertTrue(o.string == "123");

public void test2() {
  Object o = new Object("456");
  AssertTrue(o.string == "456");

Let's pretend the code has a public string field that appends an argument to that string. Now test1 returns true, because it's "123", but test2 returns false because it's "123456", which means Object o still exists when test 2 executes. I didn't have any problems doing tests in Junit before, but now my object's string just accumulates for each test.

Aren't objects supposed to be automatically destroyed when a java method finishes execution? Can I force an object to be deleted?


I removed the keyword "static" from everything in this object's class and it no longer fails. However, I don't understand why this makes a difference. Shouldn't java destroy things not being used? I also have the main method non-static, which doesn't really make sense to me...

Can anyone explain to me what's happening?

Thanks :D