• Post Reply Bookmark Topic Watch Topic
  • New Topic

singleton pattern/class life-cycle  RSS feed

 
justin mcsporran
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi gurus,
This is my first post, so pls be gentle.
I tecently finished Sun's SL-275 course and I'm still a bit confused on some issues. This week, it's the Singleton design (pattern briefly introduced on the course).
Here's the code:

What I'm struggling with is: If, in AnOther class (not shown), I call Company.getCompany(), when (and how) is the Company class instantiated in order for line 7 to work? getCompany() doesn't instantiate Company and the reference called 'instance' is static. I'm guessing that there's some gap in my understanding of the actions of the classloader and/or the class life-cycle (in particular wrt the creation of static references).
My only inkling is that perhaps it is something to do with the fact that I would have to include shipping.* in AnOther to have access to the Company class.
Apols for the long-windedness and thanks in advance for any help.
Any suggestions for further reading (online or paper) on class/object life-cycles would be also be really appreciated.
- Justin
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason this works is quite simple: static code resides "outside" of an instance of an object; you can think of static code as belonging to the class rather than to an individual object. (This is the case in Objective-C, BTW; there are "class methods" and "instance methods;" Java doesn't quite have the same concept, but it's similar enough to make the comparison).
Therefore, no instance needs be present to call static code.
If you are wondering when other things happen:
1). The company class is loaded when another class first needs to reference the Company class (either in code or in an import statement). (You could say the Class is instantiated if you wanted to think like that, but ususally it is refered to as "loading" the class).
2). The "instance" variable is created when the Company class is loaded.
Hope that this make things clearer,
 
Dirk Schreckmann
Sheriff
Posts: 7023
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch, justin!
I'm moving this to The Intermediate Forum...
 
Layne Lund
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
As noted, static members belong to the class. As it is written above, the variable instance is initialized when the Company class is loaded. I don't know the exact details, but I'm sure it's safe to assume that the class loader loads the Company class before, or at the time of, calling the getCompany() method.
HTH
Layne
 
justin mcsporran
Greenhorn
Posts: 15
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Joel and Layne, many thanks for the info and for making my debut on the Big Moose Saloon a satisfying one.
Your responses make me wonder now at what point the Company class is loaded in this pattern.
I can see three possibilities:
1. At the import statement I'd have to use in AnOther class to get access to the Company class
2. the declaration in AnOther of a variable of type Company; or
3. the initialisation of said variable with the call to getCompany().
Any thoughts on which of these would be the case?
Thanks again,
Justin
 
Joel McNary
Bartender
Posts: 1840
Eclipse IDE Java Ruby
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I believe that the class is loaded at the import statement. This is why it is preferable to say:

The former forces only the List class to be loaded, while the latter forces all classes in the package to be loaded.
(Not that loading the entire util package is a problem, since in a reasonably-sized program most, if not all, of the package would be loaded anyway...but the concept holds true for any other package).
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!