for(int i = 0; i < 3; i++){
Object obj = new Object();
}
Will 3 objects be created or only one object which gets re-instantiated 3 times?
What happens under the hood?
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Three objects will be created, because
new Object()will be called three times.There’s no such concept as an object being “re-instantiated”.
Now the same stack space may well be used to store the reference returned from the constructor, so you could argue that in some ways there’s only one variable1, which is reinitialized on each iteration of the loop… but variables and objects are very different, and it’s important that you separate the two concepts in your mind.
1 In other ways there really are three separate variables, so please don’t take this too far.