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.
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?
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
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.