• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

K&B: Has-a relationship

 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi there, I'm studying for the SCJP 5 which I'm going to take tomorrow (so nervous!)

I have a question about the "has-a" relationship. According to K&B Chapter 2, page 164, the Self-Test answers for question 1 says that "Has-a relationships always rely on instance variables". This is option B, and is marked as being the correct answer.

I distinctly remember a very similar question on one of the Master Exams that came with the book, and it said that the above was not true because Has-a relationships can also apply to static variables, which are CLASS variables, not instance variables.

I skimmed through the K&B errata but didn't find anything on it.

I realize this may be a bit trivial but since I did see a question on the practice directly asking this, I was wondering if anyone had any insight.

Do static variables count as Has-A?

EDIT: I'm working with K&B for 1.5, just to be clear
[ August 31, 2006: Message edited by: Sara Haider ]
 
Ranch Hand
Posts: 809
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Do static variables count as Has-A?



Yes. Static or non-static. In both the cases, there is a whole/part relationship.

Also a class can contain arrays of some type as well say Vehicle class contain an array of Wheel. Still there is HAS-A relationship between the Vehicle and the Wheel.

Does that help?

All the best for the exam


- Naseem
[ August 31, 2006: Message edited by: Naseem Khan ]
 
Ranch Hand
Posts: 152
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yes, has - a relationship applies also to static variables.

What the author mean with instance variables I think is to tell you:

"Applies to instance variables, which includes static instance variables, but doesn't apply to local variables(inside methods)"
 
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This is an old topic, but it is still relevant since questions from K&B 5 were apparently reused for K&B 6. I ran into this same issue with the latest K&B Java 6 book with MasterExam software. As stated already one of the practice tests makes a clear distinction between instance variables and class variables and that they both can be used in composition. This is also supported by the Java Turorial: http://download.oracle.com/javase/tutorial/java/nutsandbolts/variables.html. However, a later practice test question has the following reportedly correct answer:

"B: Has-a relationships always rely on instance variables."

This isn't right...

Note: the MasterExam software doesn't tell you what the question number is when you're reviewing the "study guide" it prints after you finish the test so I don't have the question number readily available, but I do know it came from the third practice test (test C). The study guide also doesn't show you what the answer choices were and MasterExam crashes if you minimize the window and then put the computer to sleep, but that's another topic.
 
Ranch Hand
Posts: 47
Redhat Java Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


what would be correct answer here ?
a) main has a abc
b) main has a xyz
 
Bartender
Posts: 543
4
Netbeans IDE Redhat Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
both, because an xyz IS-A abc

Though if you could pick one, pick ABC, because you can't use xyz as an xyz unless you cast it.

Hmm, main definitely has an abc, so that is definitely correct. But it doesn't necessarily have an xyz, because the value of a1 doesn't have to be xyz.
 
Ryan Slominski
Ranch Hand
Posts: 33
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
This topic is about the discrepancy on the MasterExam practice tests and whether has-a relationship applies to class variables or not (the last two posts are off-topic).

I've found the answer I was looking for and I'm updating this thread since it was the first hit on my Google search and others may find this thread as well.

After doing some more searching on this forum it turns out this has been discussed many times before and the best response appears to be from the book's author (Bert Bates): https://coderanch.com/t/424673/java-programmer-SCJP/certification/Master-Exam-Error

In summary: has-a relationships do apply to class variables as well as instance variables, though it is acknowledged as a gray area. Bert also indicated that it is unlikely this distinction will arise on the real exam.
 
reply
    Bookmark Topic Watch Topic
  • New Topic