Cannot find symbol

I have a Scala application with one Java source file. In the Java file, I have something like this:

package trajspec;

import scalar.;
import ATMunits.
;

public class AirspeedCalcs {

public final static double scalarUnitTestDummy = sec;
...
}

And when I try to compile it, I get this:

[error] /home/rpaielli/trajspec/src/main/java/AirspeedCalcs.java:8: cannot find symbol
[error] symbol: variable sec
[error] location: class trajspec.AirspeedCalcs
[error] public final double scalarUnitTestDummy = sec;

I do not understand the problem since I have been doing the same thing in Scala syntax for years with no problem. What am I missing? Why would the symbol be recognized in the Scala source files but not the Java file? Any ideas? Thanks.

Yes, the wildcard asterisks somehow got lost in my import statements above.

It just occurred to me that my second post may have led readers to believe that I found the source of my problem. No, I didn’t. The wildcard asterisks were in my code, but they got lost when I copied it to the post.

Any ideas what the problem could be?

What’s the definition of sec?

Just a guess: maybe sec is a static field. In Java, you import those
with import static, not import.

sec (unit of time) is contained in ATMunits, which is a package object. Should that be a problem? Thanks.

Thanks for the suggestion, but adding “static” didn’t work. ATMunits is a package object, so perhaps I do need a static import, but apparently there is another problem as well.

Please take a look at how Scala objects are compiled to Java classes https://www.cakesolutions.net/teamblogs/scala-dissection-basic-types

You need import static ATMunits.package$.* to import the package object’s members, or use ATMunits.package$.MODULE$.sec() to access sec specifically.

Thanks, but it doesn’t seem to work for me. What am I missing?

import static ATMunits.package$.MODULE$.sec;

public class AirspeedCalcs {

public final static double scalarUnitTestDummy = sec;
...

[error] /home/rpaielli/trajspec/src/main/java/AirspeedCalcs.java:4: cannot find symbol
[error] symbol: class MODULE$
[error] location: class ATMunits.package$
[error] import static ATMunits.package$.MODULE$.sec;
[error] /home/rpaielli/trajspec/src/main/java/AirspeedCalcs.java:4: static import only from classes and interfaces
[error] import static ATMunits.package$.MODULE$.sec;
[error] /home/rpaielli/trajspec/src/main/java/AirspeedCalcs.java:8: cannot find symbol
[error] symbol: variable sec
[error] location: class trajspec.AirspeedCalcs
[error] public final static double scalarUnitTestDummy = sec;
[error] (compile:compileIncremental) javac returned nonzero exit code
[error] Total time: 9 s, completed May 29, 2018 9:00:50 PM

val sec compiles down to a private field and a method-accessor, through which it can be accessed: sec().

Another issue that compiler reports is MODULE$ being a static field, not a class. As such, it can’t be used to import members from object that it references

To summarize,

import static ATMunits.package$.MODULE$;
...
public final static double scalarUnitTestDummy = MODULE$.sec();

Thanks, that works. But now I would like to have wildcard access to all the units, and that does not seem to work.

import static ATMunits.package$.MODULE$.*;

public class AirspeedCalcs {

//public final static double scalarUnitTestDummy = 2.0 * MODULE$.sec();
public final static double scalarUnitTestDummy = 2.0 * sec();

[error] /home/rpaielli/trajspec/src/main/java/AirspeedCalcs.java:4: cannot find symbol
[error] symbol: class MODULE$
[error] location: class ATMunits.package$
[error] import static ATMunits.package$.MODULE$.*;
[error] /home/rpaielli/trajspec/src/main/java/AirspeedCalcs.java:9: cannot find symbol
[error] symbol: method sec()
[error] location: class trajspec.AirspeedCalcs
[error] public final static double scalarUnitTestDummy = 2.0 * sec();
[error] (compile:compileIncremental) javac returned nonzero exit code

Sorry, but you can’t do that for the reason mentioned earlier.

Thanks again. Perhaps I can get that wildcard functionality if I convert the ATMunits package object to a class.

You’ll never be able to wildcard-import /members/ of object instances
(import someValue.*;) in Java. There, you can only wildcard-import
types from packages. (import java.util.*;)

Of course, it’s been years since I wrote much Java, so I could easily be
wrong.

You might be able to use the import static mechanism if you turn your package objects into normal objects.