Hi guys. I have a couple of questions from an official practice exam I did tonight. Here they are:
1) We all know that static variables are not serialized. When serializing and deserializing an object in the same execution, the static variables don't change their values, isn't it?
The answer implied that they were reset to their initial declaration value, and I'm starting to think it was an errata.
2) Another answer claimed that a composition scheme where a Class A holds a reference to an instance of another Class B, and at the same time class B has an instance of class A, is tight coupling. Reading from chapter 2 in K&B's book, it is said:
Tight coupling is the undesirable state of having classes that break the rules of loose coupling.
So if we read those rules, they say:
Loose coupling is the desirable state of having classes that are well encapsulated, minimize references to each other, and limit the breadth of API usage.
In the question, classes A and B were well encapsulated (private references, only setters with package-default access modifier), and no method calls over the instances were made (and of course no inner variables were accesed from outside the class). Why are then considered tigthly coupled? Should I consider tight coupling every composition (has-a relationship) where references are not interfaces? Or even with interfaces it is considered tight coupling? Do you have a rule of thumb for this kind of question?
hi , let me try to explain it , but need to mention, i am a java rookie , hahaha
1) do you have the question? please post it here, so we can help you figure out the answer. but as you said, serialization has no effect on static variable. but it is possible the constructor is changing the value of static variables. this is my guess so please post the question here.
the answer is follow:
normally i ask myself, if i modify object A, does it affect the object B? is the answer is yes, then it is tight coupling.
actually the definition of high or low coupling is vague , so for the exam, you can spot the keyword in order to score the question. (like the sample above.)