Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder1.hal-mli.net!border3.nntp.dca.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: borrowing Constants Date: Fri, 23 Sep 2011 19:11:56 -0700 (PDT) Organization: http://groups.google.com Lines: 43 Message-ID: <26090658.2452.1316830316920.JavaMail.geo-discussion-forums@prfh23> References: Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 24.6.43.78 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 X-Trace: posting.google.com 1316830409 26509 127.0.0.1 (24 Sep 2011 02:13:29 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sat, 24 Sep 2011 02:13:29 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=24.6.43.78; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8253 Roedy Green wrote: > If you have some code like this: > > class A > { > static String VERSION = "1.0b"; > } > > and > class B > { > static String VERSION = A.VERSION; > } > > When you use Class B, does all of class A get instantiated? Strictly speaking, and I am fairly sure you don't mean this, a class with only static members might never, or cannot ever be instantiated (depending on the use of certain idioms). As you know, I favor exactitude, some think colo-rectitude in certain terminologies. It cuts both ways - I have to relearn terms when I have them wrong. That aside, you no doubt refer to the evil twin processes of class loading and class initialization. Without going all lawyerly, let's just note that there is a precise order of those stages and rather ornate rules to them. Which all boil down to that you get a class all or nothing. So yes, you will need the entire class in both the build and runtime paths of its clients. > Does all of class A get put in B's jar? Short answer: Yes, it would have to. Classes are loaded in what the Java Language Specification (JLS) calls "compilation units". For this question, the key word is "unit". Under certain circumstances you get more than one class at a time from the same compilation unit. These come all or nothing all together or not at all. > If so, it suggests you are better off to have a tiny common class and > have A and B reference it. That's debatable for certain scenarios, but yes, absolutely it would suggest that otherwise. The niggle is that such a rule shouldn't be too restrictive. Placement of members flows from architecture and design. You get a few utility classes, sure, but don't overdo it. Things belong where they belong. -- Lew