FRIHOST FORUMS SEARCH FAQ TOS BLOGS COMPETITIONS
You are invited to Log in or Register a free Frihost Account!


N00b Java Question





coreymanshack
Code:
import javax.swing.JFrame;

   public class SimpleFrame extends JFrame {
   
   public SimpleFrame() {
      super("First Window");
      setSize(300, 100);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
   }

   public static void main(String[] arguments) {
      SimpleFrame sf = new SimpleFrame();
   }
   
}


I have installed java jdk se on my system, I have set the path variable correctly, I go to compile this program and I get the error

firstframe.java:3: class SimpleFrame is public, should be declared in a file named SimpleFrame.java
public class SimpleFrame extends JFrame {
___________^
1 error



What have I done wrong?
MrBlueSky
You have to put that code in a file called SimpleFrame.java.
Star Wars Fanatic
MrBlueSky is correct, the code needs to be in a file named SimpleFrame.java. When I tested your code, it worked fine for me. So there are no other errors besides that.
coreymanshack
wow thx, like I said, I'm a java n00b! I'll have further questions later and I hope you guys will be here to help me.

Could I get an explanation as to why my file has to be named the same as the containing class?
Star Wars Fanatic
You don't always have to have it so, the only way to not have it so is to declare the class to be non-public. The reason is a design decision by Sun. If you have the class name different from the file name, it can cause problems when you are coding and forget the name or something.

And feel free to ask any questions... I would be glad to help, lol.
coreymanshack
Star Wars Fanatic wrote:
You don't always have to have it so, the only way to not have it so is to declare the class to be non-public. The reason is a design decision by Sun. If you have the class name different from the file name, it can cause problems when you are coding and forget the name or something.

And feel free to ask any questions... I would be glad to help, lol.


Ok I tried not declaring public at the beggining of the block of code, and I got a classdefnotfounderror at runtime not compile time(more than likely because I cannot access a non public class from within my main class), so is there a way around this?

O, and here is some new code that I tried that out on.
Code:

import javax.swing.*;

   class SimpleFrame extends JFrame {
   
   JButton load = new JButton("Loading");
   
   public SimpleFrame() {
      super("First Window");
      setBounds(300, 100, 100, 100);
      setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
      setVisible(true);
      JPanel buttonshere = new JPanel();
      buttonshere.add(load);
      add(buttonshere);
   }

   public static void main(String[] arguments) {
      SimpleFrame sf = new SimpleFrame();
   }
   
}
makli
The class which contains your "public static void main()"-method has to be public. Otherwise, the runtime system cannot find the main-method as the entry point of your program.

HTH

Makli
Star Wars Fanatic
I forgot about that small detail Embarassed If you are directly running a class you have to have it public. The only other way is if you have an abstract class I think... You can also have a class declared as private, or protected, but you would need to have another public class in the file to be able to run it.
coreymanshack
Quote:
I forgot about that small detail If you are directly running a class you have to have it public. The only other way is if you have an abstract class I think... You can also have a class declared as private, or protected, but you would need to have another public class in the file to be able to run it.


Ahh ok, well that pretty much sucks, but I guess it can be a good thing, keeping names and such in line.

Quote:
The class which contains your "public static void main()"-method has to be public. Otherwise, the runtime system cannot find the main-method as the entry point of your program.

HTH

Makli


I see. Smile
Related topics
A question from a n00b
Java download question
n00b mySQL question :D
Where to find Java Scripts
Question for any guitar players out there.
Java Has Failed!
HTML/Java Question
Whos is the FUTURE.... JAVA or .NET
Java Support
java help
Java Servlet IDE and development Environmnet
phpBB 3 RC5 very basic/n00b question
ASP session.timeout question
java question
Reply to topic    Frihost Forum Index -> Scripting -> Others

FRIHOST HOME | FAQ | TOS | ABOUT US | CONTACT US | SITE MAP
© 2005-2011 Frihost, forums powered by phpBB.