I’m really new to Scala, and I’ve come across an error I am unable to solve by myself or through internet searches.
I have a Scala class called “GUI” that represents a JFrame along with a companion class. When I try to import the companion class using import GUI._ I get the error “stable identifier required, but GUI.this.GUI() found”.
I made an empty class and companion object and the import worked fine, so I assume that the error is related to something specific to my code. Below is the code in question:
object GUI {
def test:Integer = 1
}
class GUI extends JFrame{
import GUI._
val ICON_LOCATION:File = new File("Images/iMovies.ico");
val ICON:BufferedImage = Ootil.createImage("iMovies.png");
val TITLE:String = "iVideos";
val LICENSE_NAME:String = "OpenBSD";
def GUI(){
setLayout(new BorderLayout());
createGUI();
pack();
setSize(100,100);
setLocationRelativeTo(null);
setVisible(true);
}
def versionMajor: Integer = 1
def versionMinor: Integer = 0
def versionRevision: Integer = 0
def versionPreReleaseID: String = "alpha"
def versionBuildNumber: String = "1b"
private def createGUI():Unit = {
val panel = new JPanel();
panel.setLayout(new BorderLayout());
add(panel, BorderLayout.CENTER);
}
def getIcon():BufferedImage = ICON
def getProgramTitle():String = TITLE
def getConfigOptions():LookAndFeelConfigurationOptions = GUIConfigOptions.CONFIG_OPTIONS;
}
To add to Kipton’s answer, there’s nothing wrong with doing:
But the result won’t be a constructor — it will be an ordinary method.
val a = new GUI()won’t print anything, but callinga.GUI()will.This is why you didn’t get an error message about defining your constructor incorrectly.
When you run the command
import GUI._, Scala needsGUIto always evaluate to the same object. This is only the case whenGUIis anobject, apackage, or aval.In your code,
import GUI._referred to the methodGUIthat you defined, because theGUImethod is defined in a closer scope thanobject GUI(the fact that the compiler hasn’t encountered the definition ofdef GUIyet doesn’t make a difference).Since
import GUI._referred to the methodGUI, which is not aval,object, orpackage, you got the error message aboutGUInot being a stable identifier.