This week's book giveaway is in the Jobs Discussion forum.
We're giving away four copies of Java Interview Guide and have Anthony DePalma on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Problem with Coupling Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of Java Interview Guide this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Problem with Coupling" Watch "Problem with Coupling" New topic

Problem with Coupling

Richa Garg

Joined: May 28, 2008
Posts: 5
Dear all,

I am having some problem in understang coupling.Please clear my doubts.
I am creating two classes that are in same package.
Variables of Class A are private but method has default access.
I am using that method in another class through its class reference variable . Are they coupled or loosely coupled or tightly coupled.


Class A{
private int a;
int getA(
return a;
Class B{
private int b;
public void get y(A a1){
Stevi Deter
Ranch Hand

Joined: Mar 22, 2008
Posts: 265


In your example, B is fairly tightly coupled to A.

B's behavior is dependent on A. If A#getA() changes, it affects what happens in B#gety().

At risk of confusing you further, I'd point out this example is generally acceptable under the Law of Demeter, as B#gety() is using "only one dot" of A.

But a better design in this specific example would probably be to refactor B as such:

This decouples B from A.

If B really need to consume data from A to perform its behavior, then it's fine to pass in the object and use the "only one dot" level of coupling. But in this example you give, the coupling doesn't appear to be achieving anything, Which is often the case in the sort of arbitrary examples we see when first learning!
[ May 28, 2008: Message edited by: Stevi Deter ]

There will always be people who are ahead of the curve, and people who are behind the curve. But knowledge moves the curve. --Bill James
Jolly Tiwari
Ranch Hand

Joined: Mar 26, 2006
Posts: 77
Richa ,

Can you share the source of your questions (link to online mock exams)as that would help all ,preparing for the exam and save our time looking for free online mock exams on internet.


I agree. Here's the link:
subject: Problem with Coupling
It's not a secret anymore!