Sunday, August 18, 2013

Confuse what is the difference between two cases?

Confuse what is the difference between two cases?

My question is related to this SO question.
My first question is as far as I can understand number of objects are 3,
s1, s3 and "abc" (I posted an answer to this question). If some one says
it is 4. How that happens there?
consider my following class.
public class ObjectCount {
static int i;
ObjectCount() {
System.out.println(++i);
}
public static void main(String args[]) {
ObjectCount obj1 = new ObjectCount(); //i=1
ObjectCount obj2 = new ObjectCount(); //i=2
ObjectCount obj3 = obj1; // obj3 is also an object refer same as obj1
}
}
How many objects are there? again I think it should be 2. So how above SO
post answer can be 4 objects?can some one help me to figure it out where I
get some point incorrect?
FYI : Now previous issue has fixed on that post. Incorrect answer has made
correct. It is 3 object there. No need to worry.

No comments:

Post a Comment