• Post Reply Bookmark Topic Watch Topic
  • New Topic

Unknown Class but the codes looks ok  RSS feed

 
Greenhorn
Posts: 1
IBM DB2 IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Okay, so I got stuck with the error "Unknown class" with the code that I copied from the book "Java: A Beginner's Guide, Sixth Edition". I thought of asking here since I got grilled bu the "pros" from another forum on not studying the basic Java first.

I'll try my luck on this forum...



The error comes from lines 21 and 27.

21 MAIN_FRAME.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
27 MAIN_FRAME.add(FIELD_LABEL);

I have tried to rewrite from scratch and still gets the same error.
 
author & internet detective
Marshal
Posts: 37518
554
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thomas,
Welcome to CodeRanch!

This code is a problem:


In particular, Java doesn't allow random loose code in a class. Perhaps you want to move that code into your main method? (There are other ways to do it like a static initializer or constructor. Don't know how much Java you know yet.)
 
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Thomas,

I'll just add that you should reconsider the variable names you are using. Names in all capitals, with underscores separating the words, are generally used only for final (constant) variables, such as the JFrame.EXIT_ON_CLOSE constant. Using FIELD_LABEL for a non-final variable can be confusing for others reading your code. Generally, you want to use lowerCamelCase style (as discussed in the Oracle Java Coding Conventions), or the styling conventions in the JavaRanch Coding Style Handbook.

I know it can be frustrating to try and remember all those things when you are starting out, but trust me - it's easier to create good habits, then it is to try and break bad habits later on.
 
Greenhorn
Posts: 7
Firefox Browser Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I too would suggest first learning the basics, I think GUI programming is far out of your range. You can easily and efficiently learn from Oracle themselve: http://docs.oracle.com/javase/tutorial/

However I've cleaned up the code for you so you can learn from your mistakes:


I have not tested it, but it should work.
 
Chris Barrett
Bartender
Posts: 321
24
Eclipse IDE Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks for the cow, oh great and mighty Cow Gods!
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!