• Post Reply Bookmark Topic Watch Topic
  • New Topic

Cant understand generics problem.  RSS feed

 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Generic class:



Main:



Why is the output "2 2" and not "1 1"

EDIT: Fixed my main so it prints out.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:Generic class:



Main:



Why is the output "2 2" and not "1 1"



There are no output statements whatsoever from the classes that you posted.

Henry
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry, I fixed it now.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:
Main:



Why is the output "2 2" and not "1 1"

EDIT: Fixed my main so it prints out.



Basically ... the rules regarding static and instance variables don't change in regards to generics (or perhaps, "no special handling for generics" is a better phrase to describe it). There is only one copy of the static variable for the Container class.... which of course, is a little different, if you are used to C++ templates.

Henry
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Maybe I missed your point but I still don't understand why that would print out "2 2" ... You only call the add method once and the initial values are 0 so where are the extra 1's coming from?
 
Paul Clapham
Sheriff
Posts: 22835
43
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Bob Ivanovich wrote:You only call the add method once and the initial values are 0...


No, that's wrong. The "count" variable is static so there is only one initial value (which is zero). And you call the add method twice, once on each Container instance, so that increments it twice.

Like Henry said, the generics aspect of the code is irrelevant. Just consider the rules for static variables (as I did there) and you'll see why you get the answer you get.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paul Clapham wrote:
Bob Ivanovich wrote:You only call the add method once and the initial values are 0...


No, that's wrong. The "count" variable is static so there is only one initial value (which is zero). And you call the add method twice, once on each Container instance, so that increments it twice.

Like Henry said, the generics aspect of the code is irrelevant. Just consider the rules for static variables (as I did there) and you'll see why you get the answer you get.



Admittedly, when I read this topic, my first thought was another person confusing the subtle differences between Java generics and C++ templates.... but perhaps, I assumed a bit too much.


Taking a step back, can the OP explain to us the difference between a static and instance variable? Perhaps, this is a (simpler) case of not understanding the difference between a static and instance variable.

Henry

 
Alexandru Dragoi
Ranch Hand
Posts: 49
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Bob,

Because variable count is static, it does belong to the Container class. Every instance (thus, also c1 and c2) will share the same count.
The order of the operations is like this:
1/ c1.add() will increment count to 1;
2/ c2.add() will increment the same count (shared with c1 and with every other instance of Container class). The variable value will become 2.

Take a look on this Java tutorial for more information:
http://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html

Because add and getCount methods access only static members you could make them also static. There is no reason to have a reference to an object in order to access these 2 methods.

Best regards,
Alex
 
Bob Ivanovich
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot about the 'static' when I was going through this. Thank you everyone.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!