Read pages 1
and then type or write answers to these questions:
How do you create a global variable in Java?
Page 170 says that a downside to a global variable in Java is that you have to create the object when the
rogram first begins. Technically, that is not true. You can declare in class A, static B b. Then, anytime
later (anywhere) you can instantiate b, with code like this: A.b = new B(). But that defeats the purpose
of having a global variable. Why?
Study the s
ingleton code on page 173 very carefully. Understand it completely. Suppose you need a
class to store data about access attempts on any system mass storage device. The class should have
method and a
method. Since the log will be utilized
system wide, it need to be a
singleton. Write the code for the
class trying hard to rely on your memory of how it works and not
peeking at the text unless you have to.
On page 177, what does this mean, “...the Singleton is created in a lazy manner...?”
What are the
While we say that lazy creation is advantage, once we the first request is made, the singleton exists
forever. Suppose this singleton occupied a large amount of memory which causes a slow
down in the
system when it is
not needed. Often, long stretches of time go by when the singleton is not needed.
Then, it may get hundreds of hits in a brief time. What change could you make to the classic singleton
design to help this situation.
Be the JVM
. Explain why the approach to
singleton presented so far, can fail with multiple threads.
Why is the multiple threads problem a somewhat limited problem?
How do we fix the multithreading problem? What is the disadvantage of this?
Describe three ways to improve the multithreading single
What does the volatile keywork in Java mean?
Why do the authors warn against
simply making all methods and variables static?
We have a new design principle in the first paragraph on page 185. What is it?
What are the two responsibilities that the sing
In terms of the singleton pattern, why are global variables bad?