Reputation: 23
So this is my game...
import javax.swing.JFrame;
public class Main extends JFrame {
//the parts of the game like the parts in a desktop
public void Skeleton() {
//entry point of the game
add(new Board());
// sets the title
setTitle("Skeleton");
// makes it safe for the program to close without leaks
setDefaultCloseOperation(EXIT_ON_CLOSE);
//sets the size
setSize(300, 280);
//sets the location
//null means middle
setLocationRelativeTo(null);
//set the window to be visible
setVisible(true);
//allows the window to be resized if the statement is true
setResizable(false);
}
//wraps everything up
public static void main(String[] args) {
new Skeleton();
}
}
there is an error with new Skeleton();
and the error says
Skeleton cannot be resolved to a type
Upvotes: 0
Views: 138
Reputation: 40335
It appears you are trying to define a constructor for a class; but the constructor should not have a return type and must be the same name as the enclosing class. You could change the class name from Main
to Skeleton
and fix constructor syntax, e.g.:
public class Skeleton extends JFrame { // <- name changed
public Skeleton () { // <- no 'void'
...
}
public static void main(String[] args) {
Skeleton s = new Skeleton();
// do stuff with 's'
}
}
You could do the same thing but keep the name Main
:
public class Main extends JFrame {
public Main () { // <- no 'void'
...
}
public static void main(String[] args) {
Main m = new Main(); // <- use 'Main' not 'Skeleton'
// do stuff with 'm'
}
}
Or you could just not use a constructor and leave your code as is, but call Skeleton()
as an instance method, as suggested in the other answers here, e.g.:
public class Main extends JFrame {
public void Skeleton () {
...
}
public static void main(String[] args) {
Main m = new Main();
m.Skeleton(); // <- `Skeleton()` is an instance method
// do stuff with 'm'
}
}
... but looking at your code (typically that kind of work is done in a constructor) and guessing at your new Skeleton()
intentions, it seems like one of the constructor examples above is what you were going for. Performing initialization in a separate method after construction is generally not the greatest way to do things (sometimes there are situations where this is useful, but for the most part it keeps the code cleaner and clearer, and avoids possible mistakes like forgetting to initialize the class after you construct it).
Upvotes: 2
Reputation: 14707
As mentioned by Jason C
and Paul Richter
that Skeleton is a method
not a class
so you can not initialize Skeleton()
. So either change your class to Skeleton
or create Object of Main
and call the method. I just wanted to add one more thing that all swing should be called using SwingUtility.invokeLater()
so you can do something like this.
public static void main(String[] args) {
SwingUtilities.invokeLater(new Runnable() {
public void run() {
Main frame = new Main();
frame.Skeleton();
}
});
}
Upvotes: 1
Reputation: 11072
Your Skeleton
is a method, not a class. Only classes can be instantiated. I believe what you're trying to do is this:
public static void main(String[] args) {
Main mainFrame = new Main();
mainFrame.Skeleton(); // I assume you were trying to ultimately call this method.
}
Side note: In Java, the standard is for methods to begin with a lower case letter, so your Skeleton
should be skeleton
. Both are technically, syntactically valid, however the latter, as I said, is the standard, and it makes it easier for others (or even yourself) looking at your code to read and understand it quickly when the standard is followed. In practical terms, I find it makes it hard to know when you're looking at a class, method, or object, when you mix cases like that.
Edit: And as JasonC pointed out, the StackOverflow syntax highlighter gets mightely confused when there's incorrect cases, which speaks to what I mentioned in the previous paragraph, that it makes it a bit more difficult for others to read (especially at a glance).
Upvotes: 2
Reputation: 41200
new Main().Skeleton();
^^^^^ ^^^^^^^^^
Class Name instance method.
what you eaxtly need to write.
Skeleton
is pubic method which you can access by instance of that class. In java new <className>()
create a new object of that class.
Code snippet :
Main main = new Main();
main.Skeleton();
Upvotes: 0