• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Paul Clapham
  • Jeanne Boyarsky
Sheriffs:
  • Devaka Cooray
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Moores
  • Ron McLeod
  • Tim Holloway
  • Claude Moore
  • Stephan van Hulst
Bartenders:
  • Winston Gutkowski
  • Carey Brown
  • Frits Walraven

Class Relationship Question  RSS feed

 
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
What class relationship do AAA and BBB have in the code below?




I would say composition. With the argument that class AAA contains objects of the class BBB.

Apparently not, the right answer should be dependency or association.

How come it's not composition?
 
Bartender
Posts: 5642
56
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"_b" is not a member variable (aka field), therefore it is not composition. You can't say that AAA has-a BBB. "_b" comes into existence inside AAA's constructor, but is only used as a local variable, and then disappears at the end of the constructor. I would say that the constructor "uses" BBB, or that the constructor perhaps "depends" on BBB, but the class AAA does not really depend on BBB. Once an AAA is constructed, BBB is not a part of the AAA object any longer.

Also, the use of an underscore (_) in a variable name is highly frowned upon. I've only seen this in much older projects, and even those are being undone as they get re-factored.
 
Daniel Andersson
Ranch Hand
Posts: 104
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Carey Brown wrote:"_b" is not a member variable (aka field), therefore it is not composition. You can't say that AAA has-a BBB. "_b" comes into existence inside AAA's constructor, but is only used as a local variable, and then disappears at the end of the constructor. I would say that the constructor "uses" BBB, or that the constructor perhaps "depends" on BBB, but the class AAA does not really depend on BBB. Once an AAA is constructed, BBB is not a part of the AAA object any longer.

Also, the use of an underscore (_) in a variable name is highly frowned upon. I've only seen this in much older projects, and even those are being undone as they get re-factored.


Ok thanks, good explanation! (y)
 
Sheriff
Posts: 13176
219
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That code will not even compile. You cannot have any private declarations inside a method or constructor.
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
Become a Java guru with IntelliJ IDEA
https://www.jetbrains.com/idea/
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!