Path: csiph.com!x330-a1.tempe.blueboxinc.net!newsfeed.hal-mli.net!feeder3.hal-mli.net!news.glorb.com!postnews.google.com!glegroupsg2000goo.googlegroups.com!not-for-mail From: Lew Newsgroups: comp.lang.java.programmer Subject: Re: borrowing Constants Date: Sun, 25 Sep 2011 11:36:32 -0700 (PDT) Organization: http://groups.google.com Lines: 56 Message-ID: <26695994.492.1316975792589.JavaMail.geo-discussion-forums@prec11> References: <4e7d27e8$0$290$14726298@news.sunsite.dk> <86afq.26627$CY4.6386@newsfe04.iad> <02dq77p0i00lsfdsl78bfnencd2ab4a5gs@4ax.com> Reply-To: comp.lang.java.programmer@googlegroups.com NNTP-Posting-Host: 216.239.45.130 Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Trace: posting.google.com 1316975905 5492 127.0.0.1 (25 Sep 2011 18:38:25 GMT) X-Complaints-To: groups-abuse@google.com NNTP-Posting-Date: Sun, 25 Sep 2011 18:38:25 +0000 (UTC) In-Reply-To: Complaints-To: groups-abuse@google.com Injection-Info: glegroupsg2000goo.googlegroups.com; posting-host=216.239.45.130; posting-account=CP-lKQoAAAAGtB5diOuGlDQk0jIwmH0T User-Agent: G2/1.0 X-Google-Web-Client: true Xref: x330-a1.tempe.blueboxinc.net comp.lang.java.programmer:8307 On Sunday, September 25, 2011 11:25:49 AM UTC-7, Daniel Pitts wrote: > Roedy Green wrote: >> The question is really about accidentally dragging in some giant class >> when you had no intention of using its code. >=20 > If you are using a field from it, you *are* using its code. >=20 > Imagine the following scenario: >=20 > public class MyConstants { > public static final String VERSION; > public static final int ZERO; > static { > ZERO =3D 0; Compile-time constant. > VERSION =3D SomeOtherClass.calculateVersionNumber(); Not a compile-time constant. They are treated differently. > } > } >=20 > public class UsesOtherClass { > public static final VERSION =3D MyConstants.VERSION; > } >=20 > When UsesOtherClass is initialized, it requires MyConstants *and*=20 > SomeOtherClass to be initialized. But it compiles the compile-time constant in at compile time (hence the nam= e, "compile-time" constant - amazing connection, eh?), so it doesn't need t= he source class at run time for that constant. > In general, if you use some code, you are transiently using its=20 > dependencies as well. The exception being compile-time constants. > Although, I thought I read somewhere that constants can sometimes be=20 > inlined (which can lead to other surprising behaviors if you recompile=20 > only some of your classes on make). More precisely, it's *compile-time* constants that get special treatment. = People bandy about the term "constant" very carelessly. Compile-time constants are bound in at compile time (the logic is compellin= g) as as literals. The surprising behaviors come at runtime from version m= ismatches and from the fact that transitive class dependencies are hidden b= y the compile-time constants having been bound at compile time. --=20 Lew