1

I want to begin to learn about GUI in java. However, when i try to copy a simple JFrame code from a tutorial website to Textpad and when I try to compile it, there is an error:

"C:\Programming\Java\Practice GUI\GUIPractice.java:7: error: invalid method declaration; return type required public MyFrame() { ^ 1 error"

This also happens when I also copy simple GUI code from other websites, What seems to be the problem? I know that a method must either be void or a return type, but why does the method not specify if void or if return-type, a datatype? This seems to be the syntax of GUI code for other sites.

Here is the code:

// file: EmptyFrame.java
// Adapted from Core Java, vol.1, by Horstmann & Cornell

import javax.swing.*;

class MyFrame extends JFrame {
  public MyFrame() {
    setTitle("My Empty Frame");
    setSize(300,200); // default size is 0,0
    setLocation(10,200); // default is 0,0 (top left corner)
  }

  public static void main(String[] args) {
    JFrame f = new MyFrame();
    f.show();
  }
}

I tried this with the Netbeans IDE and the same error shows up. what seems to be the problem?

4
  • Should work fine. No errors in your program Commented Feb 21, 2017 at 12:43
  • this "method" is actually a constructor and has therefore no return type. But: a constructor needs to be named exactly the same like your class. There seems to be some confusion about your naming: the error is reported in a file called "GUIPractice.java", your source code gives "file: EmptyFrame.java", the class is actually called "MyFrame" - which is now the correct file name and class name? Commented Feb 21, 2017 at 12:50
  • I'm sorry about the confusion but I copied the code directly from the website, I actually renamed the class name when I compiled to Textpad. Commented Feb 21, 2017 at 13:18
  • Thank you @ThomasKläger, Renaming the constructor actually solved the problem, But why should the constructor have the same name as the class file? Commented Feb 21, 2017 at 13:24

4 Answers 4

1

Your class is called MyFrame, but it is in a file called EmptyFrame.java. They need to be the same name. Because they are different, it thinks MyFrame() is a method.

Sign up to request clarification or add additional context in comments.

4 Comments

They just need to be named the same if the class is public (which the class in the example is not)
But then he'd get an error from having a static method (i.e. static void main() ) which is only allowed in top-level classes.
Sure, a static method is only allowed in a top-level class. But top-level classes can be public, protected or only package visible. And the class name only needs to match the file name if the class is public.
I'm sorry about the confusion but I copied the code directly from the website, I actually renamed the class name when I compiled to Textpad.
0

Change GUIPractice.java to MyFrame.java or change the class name and constructor to GUIPractice.

1 Comment

I'm sorry about the confusion but I copied the code directly from the website, I actually renamed the class name when I compiled to Textpad.
0

In every .java file, there must be a class with the same name as the file name, otherwise a ClassNotFoundException will be thrown at runtime. The error you saw is pretty weird. I don't know why it complains about the return type of a constructor.

Just rename your file so that it has the same name as the class i.e. MyFrame.java. You can also make your class public.

4 Comments

Could you provide a link where this requirement is stated? As far as I know only public classes do need to reside in files with the same name. (Though it is best practice to place classes in similarly named files it is only required for public classes)
I just tested it and a ClassNotFoundException is thrown. @ThomasKläger
I'm sorry about the confusion but I copied the code directly from the website, I actually renamed the class name when I compiled to Textpad.
Are you sure? I just tried javac GUIPractice.java, followed by java MyFrame and the frame showed up
0

The problem actually arose since After I copied the code to Textpad, I renamed the class name to GUIPractice to match the file name. However, I did not know that the method which contained the GUI Code is actually not a method but a constructor. However, what baffles me is why should the constructor have the same name as the class name.

2 Comments

Well, that is how constructors are named. The Java Language Specifiction states: The SimpleTypeName in the ConstructorDeclarator must be the simple name of the class that contains the constructor declaration, or a compile-time error occurs.
Thank you, actually, I didn't know this since we are still in the topic of Methods in our class. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.