• Post Reply Bookmark Topic Watch Topic
  • New Topic

HF Java 5 minute mystery. Constructors and Garbage.  RSS feed

 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi all,

Was a little unsure of this example in the book, so decided to compile it myself bit by bit.








now I've split these classes into class files, and added the usual public access modifiers. but I keep getting an error about unchecked operations when compiling V2Radiator. Using -Xlint explains why - the arraylist uses an unchecked type.

am I doing something wrong? or should the book declare "ArrayList<SimUnit> aList = new ArrayList<SimUnit>" in its 'TestLifeSupportSystem' class?

also, does the book just expect you to divide the code into class files, or have I missed something? combined with the unchecked operation it seems like it doesn't expect you to compile the code at all....

Thanks
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The unchecked errors are warnings because you aren't use generics. If it said ArrayList<SimUnit>, you wouldn't have that warning.

The actual compiler error is something else though. Take a look at this code and remember these three rules:
1) A constructor is like a method without a return type
2) A constructor must have the same name as the class
3) Java is case sensitive



See anything wrong?
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:The unchecked errors are warnings because you aren't use generics. If it said ArrayList<SimUnit>, you wouldn't have that warning.

The actual compiler error is something else though. Take a look at this code and remember these three rules:
1) A constructor is like a method without a return type
2) A constructor must have the same name as the class
3) Java is case sensitive



See anything wrong?


oops. thanks! although that 'r' is not in my actual code. just a typo on here

so the suggestion of ArrayList<SimUnit> is correct? i'm assuming this would have to be duplicated in the constructors for each VRad class too?
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:so the suggestion of ArrayList<SimUnit> is correct? i'm assuming this would have to be duplicated in the constructors for each VRad class too?

Oh. Then they are just warnings about not using auto-boxing.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
nick woodward wrote:so the suggestion of ArrayList<SimUnit> is correct? i'm assuming this would have to be duplicated in the constructors for each VRad class too?

Oh. Then they are just warnings about not using auto-boxing.


oh! so if you use a generic type, or forget to specify a type - in this case creating an arrayList of objects rather than SimUnits, does the compiler 'unbox' from a SimUnit to an Object?

sorry, i only know about unboxing/boxing in terms of wrapper classes, but i guess that makes sense as the SimUnit theoretically contains or is-an 'object'.

PS, if you have a sec, could you have a brief glance at my answer to Nico's question in this subforum please? feel a bit guilty..... i tried to answer it, but i have a feeling i've ruined his chances of an educated reply, because it looks like someone is helping!
I messed up
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
oh, and the 'mistake' or problem with this code the book is looking for is the inclusion of the call to the superclasses constructor in the V3Radiator. but it can't be removed without an error.
as far as i can see anyway. either the inheritance is nonsense or the work of the for loop of V3Radiator needs to be done by a method that overrides a similar method in V2Radiator. correct?

as much as i love this book, sometimes the examples are so abstract as to be completely and utterly confusing.
 
Jeanne Boyarsky
author & internet detective
Marshal
Posts: 37462
537
Eclipse IDE Java VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
nick woodward wrote:oh, and the 'mistake' or problem with this code the book is looking for is the inclusion of the call to the superclasses constructor in the V3Radiator.

What's wrong with the constructor call? I'm not seeing it.
 
nick woodward
Ranch Hand
Posts: 382
12
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jeanne Boyarsky wrote:
nick woodward wrote:oh, and the 'mistake' or problem with this code the book is looking for is the inclusion of the call to the superclasses constructor in the V3Radiator.

What's wrong with the constructor call? I'm not seeing it.


oh sorry, my bad. its to do with the context of the question. the V3Radiator calls the V2Radiator which creates another 5 SimUnits on top of those created in the V3 class, which is not what the 'scenario' wanted.

again, its such an abstract example i'm not sure its worth the time to explain! but many thanks for your help Jeanne!
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!