Multiple Same Object Instantiation

What exactly happens in Java when you instantiate the same object multiple times?

For example:

Test test = new Test();

then later on I will call it again, Test test = new Test(); again or inside a loop. Will the previous instantiation be disposed?

  1. each call new Test() creates a new object instance of the Test class
  2. test = new Test(); assigns a reference of that object to variable test
  3. A repeated statement test = new Test() creates a second instance of Test and assigns the reference it to the variable test again, so the first reference is replaced.

If and when the first instantiation will be disposed, however, depends. The Java runtime environment traces references, so as long as there are any references to an object in use, the object won’t be disposed. For example, if between step 2 and step 3 the reference to the first instantiation is stored somewhere else (like Test test2 = test;), and that variable is still in scope when step 3 occurs, then the first object is not disposed, since test2 is still holding a reference to it. However, when there is no such statement and step 3 omits the one-and-only reference to the first object, then a dispose of that object will happen.

Note that the dispose itself does not happen immediately, but sometimes later, the next time when the garbage collector is run. But that is independent from the semantics of the code discussed, thats only a question of memory and performance.

4

1: Some wording: We don’t instantiate objects at all, we instantiate classes, and the product of a class instantiation is an object (also called an instance). Of course we can instantiate a class as many times as required to create a new and different object with each instantiation.

2: Whatever, you cannot have the code you suggest:

Test test = new Test();
Test test = new Test();

neither you can have this:

for (i=0; i<3; i++) {
    Test test = new Test();
    Test test = new Test();
}

None will compile, because the part Test test is the declaration of variable test (of type Test), and no language, including Java, allows to declare twice the same variable in the same scope.

However you may assign multiple times the same variable (as the name implies), with different objects, like this:

Test test = new Test();
test = new Test();

The first line declares test and assign it an object newly created. The second line assign to test another object, also newly created.

Also note that shortcuts are allowed in Java:

for (i=0; i<3; i++) {
    Test test = new Test();
}

This loop will not declare test 3 times. It is understood as assigning three new different objects in sequence to the variable test which will be declared only once. (compare with the previous loop).

3: What happens to the variable test, and to the two objects of type Test?

The variable test is first assigned the reference of an object, then of another object. A variable retains only the last reference assigned.

The first object of type Test is not assigned to test anymore. If no other variable has been assigned its reference, then this object cannot be accessed in any way by the program, and thus is useless. It is made eligible to garbage collection by the system. At some point in time the system (the JVM) will run its garbage collector which will clear it from memory.

The second object will live as long as at least one variable references it, then the garbage collector will also take care of it and bring it to the Unreferenced Objects Cemetery. That’s the true life of Java objects 🙁

2

Short answer: all previously created objects will be disposed of if no one points to them. Only the most recent one will exist because test references it.

Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa Dịch vụ tổ chức sự kiện 5 sao Thông tin về chúng tôi Dịch vụ sinh nhật bé trai Dịch vụ sinh nhật bé gái Sự kiện trọn gói Các tiết mục giải trí Dịch vụ bổ trợ Tiệc cưới sang trọng Dịch vụ khai trương Tư vấn tổ chức sự kiện Hình ảnh sự kiện Cập nhật tin tức Liên hệ ngay Thuê chú hề chuyên nghiệp Tiệc tất niên cho công ty Trang trí tiệc cuối năm Tiệc tất niên độc đáo Sinh nhật bé Hải Đăng Sinh nhật đáng yêu bé Khánh Vân Sinh nhật sang trọng Bích Ngân Tiệc sinh nhật bé Thanh Trang Dịch vụ ông già Noel Xiếc thú vui nhộn Biểu diễn xiếc quay đĩa Dịch vụ tổ chức tiệc uy tín Khám phá dịch vụ của chúng tôi Tiệc sinh nhật cho bé trai Trang trí tiệc cho bé gái Gói sự kiện chuyên nghiệp Chương trình giải trí hấp dẫn Dịch vụ hỗ trợ sự kiện Trang trí tiệc cưới đẹp Khởi đầu thành công với khai trương Chuyên gia tư vấn sự kiện Xem ảnh các sự kiện đẹp Tin mới về sự kiện Kết nối với đội ngũ chuyên gia Chú hề vui nhộn cho tiệc sinh nhật Ý tưởng tiệc cuối năm Tất niên độc đáo Trang trí tiệc hiện đại Tổ chức sinh nhật cho Hải Đăng Sinh nhật độc quyền Khánh Vân Phong cách tiệc Bích Ngân Trang trí tiệc bé Thanh Trang Thuê dịch vụ ông già Noel chuyên nghiệp Xem xiếc khỉ đặc sắc Xiếc quay đĩa thú vị
Trang chủ Giới thiệu Sinh nhật bé trai Sinh nhật bé gái Tổ chức sự kiện Biểu diễn giải trí Dịch vụ khác Trang trí tiệc cưới Tổ chức khai trương Tư vấn dịch vụ Thư viện ảnh Tin tức - sự kiện Liên hệ Chú hề sinh nhật Trang trí YEAR END PARTY công ty Trang trí tất niên cuối năm Trang trí tất niên xu hướng mới nhất Trang trí sinh nhật bé trai Hải Đăng Trang trí sinh nhật bé Khánh Vân Trang trí sinh nhật Bích Ngân Trang trí sinh nhật bé Thanh Trang Thuê ông già Noel phát quà Biểu diễn xiếc khỉ Xiếc quay đĩa
Thiết kế website Thiết kế website Thiết kế website Cách kháng tài khoản quảng cáo Mua bán Fanpage Facebook Dịch vụ SEO Tổ chức sinh nhật