Cannot find symbol


#1

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.


#2

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


#3

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?


#4

What’s the definition of sec?


#5

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


#6

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


#7

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.


#8

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.


#9

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


#10

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();

#11

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


#12

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


#13

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


#14

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.


#15

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