Answer the question
In order to leave comments, you need to log in
Can 2 objects of the same type have the same reference name if the objects are created in a loop?
For experienced programmers, the question is ridiculous, but still I did not find the answer to it anywhere.
The loop creates 10 Song objects and adds them to the collection. Each iteration of the loop uses the same newSong reference variable.
Question: when the loop finishes its work in the ArrayList collection there will be only one object of type Song (the last one added) to which the "newSong" reference refers, and all other Song objects will be without reference?
public class Song {
}
public class Test {
public static void main(String[] args) {
ArrayList list=new ArrayList();
for(int i=0; i<10;i++){
Song newSong=new Song();
list.add(newSong);
}
}
}
Answer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question