Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > How are multiple Java files compiled together?

Thread Tools

How are multiple Java files compiled together?

Posts: n/a
Roedy Green wrote:
>> 3. It can compare the dates on the class file and corresponding *.java
>> file. If the *.java file is newer it knows to recompile.
>> see

> There is a gotcha.
> If have a static final constant X in class A.

I think it applies to instance and local constants also.

> Class B references X.
> You modify the value of X.
> The compeller will be smart enough to recompile A, but not B. It
> should recompile B, because of constant inlining.


What do you mean by that?

The constant, by virtue of it being a constant, is in the B class as a
constant. It is not a reference because it's a constant. Class B has no way of
knowing that the constant once had a label in another class. Because it's a

That's the way it is. I do not express an opinion on how it should be.

The JLS does explain why that is, though.

> When you update non-private constant values, do a clean compile of
> everything to get the inlined constant values propagated.

We create non-private constants, but we really aren't supposed to.
"Other than for true mathematical constants, we recommend that source code
make very sparing use of class variables that are declared static and final.
If the read-only nature of final is required, a better choice is to declare a
private static variable and a suitable accessor method to get its value."

BTW, public, don't confuse constants with "static final" variables. Only some
of the latter are the former, and some of the former are not the latter. The
JLS doesn't restrict the definition of "constant variable" to static
variables, or even member variables.
"A variable of primitive type or type _String_, that is _final_ and
initialized with a compile-time constant expression (§15.2, is called a
/constant variable/."

One of the implications:
"One other thing to note is that static final fields that have constant values
(whether of primitive or String type) must never appear to have the default
initial value for their type (§4.12.5). This means that all such fields appear
to be initialized first during class initialization (§, §9.3.1, §12.4.2)."
[op. cit.]

Honi soit qui mal y pense.
Reply With Quote
Gene Wirchenko
Posts: n/a
On Mon, 28 May 2012 22:35:41 -0700, Lew <(E-Mail Removed)> wrote:

>Gene Wirchenko wrote:
>> Knute Johnson wrote:
>> [snip]
>>> Did I mis-remember that or has it changed since version 4 or there
>>> abouts? I seem to remember a long thread here on this subject and I've
>>> always erased all my .class files before every compilation to avoid it.
>>> Have I generated my own myth?

>I live in the now. I don't know what you remember, nor how well, nor what changed.
>> Maybe, but I can believe you had a difficulty that needed clean
>> compiles.

>> I had a problem with jCreator where I was using code from a
>> textbook on an assignment. (Many of the exercises used textbook
>> source code.) I had to compile the textbook source code, but then
>> subsequent compilations would fail claiming that the textbook source
>> code was not valid Java code! The solution^Whorrible kludge was to
>> the delete the textbook source code used after the first compilation.

>Or put it in it own JAR.
>Or figure out why it went wrong and solve it from understanding.

Easy to say. Not necessarily easy to do.

The error message was quite misleading. The solution was by
accident. There was nothing that I could see that would help me in
tracking down the cause of the problem.

While I do like solving problems, sometimes, there is no clean

>> When things like this happen, I am not surprised that people get
>> shy. I prefer clean, full compiles myself.

This still fits.

>The Java compiler does the best it can to resolve dependencies. It can't cover
>a lot of scenarios, and it can really lose it over sometimes having source in
>the sourcepath and sometimes not. It treats constants (as the JLS defines
>them) differently from other immutable or mutable expressions.

Well, it sure seems to have done that here.

>Clean compiles are fine for your non-library (not in JARs) code and sometimes
>necessary. We're not supposed to behave by superstition, but clean compile is
>a prevalent and fairly harmless one.

It does remove the problem of loss of synchronisation.



Gene Wirchenko
Reply With Quote

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
If I create a page, then it's compiled upon first request, where cani find the compiled code?? lander ASP .Net 5 03-05-2008 04:34 PM
Text files read multiple files into single file, and then recreate the multiple files Python 4 02-13-2005 05:44 PM
g++ compiled C++ code called from gcc compiled C code Klaus Schneider C++ 1 12-02-2004 01:44 PM
newbie question about compiled code--files needed Rory ASP .Net 0 08-01-2003 04:57 AM