• Post Reply Bookmark Topic Watch Topic
  • New Topic

Problem printing a string  RSS feed

 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Greetings

This should be easy. Here is my code



and here is the compiler errors I get

com/fwh/test/Test1.java:9: error: <identifier> expected
System.out.println(className);
                                 ^
com/fwh/test/Test1.java:9: error: <identifier> expected
System.out.println(className);
                                                        ^
As you can see, the compiler doesn't like my println statement, but it looks fine to me. What am I doing wrong
 
Carey Brown
Saloon Keeper
Posts: 3315
46
Eclipse IDE Firefox Browser Java MySQL Database VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Lines 5, 6, & 8 define and initialize variables. Line 9 however does not and should be enclosed inside a method, such as main(). Also, your class Test1 should be declared public.
 
Fred Hamilton
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks.

No main method.  doh!  Excuse me while I kick myself
 
O Shea
Ranch Hand
Posts: 196
3
Java MySQL Database Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
System.out.println statement should be present inside a method because if it does not inside a method then when would that code get executed?
Since we can't execute a class we can only execute a specific method contained within that class.
The only allowed things outside method and constructor declarations are declarations of fields. Since System.out.println() is not a field declaration, so it is not allowed at all.

Also, if your code dose not contain main method that it is not a compile time error, because in your code you haven't done any syntactical mistakes. Not having a main method is a runtime exception and not compile time.
 
Campbell Ritchie
Marshal
Posts: 56536
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
O Shea wrote:. . . The only allowed things outside method and constructor declarations are declarations of fields.
Or nested types, or initialiser blocks.
. . . Not having a main method is a runtime exception and not compile time.
That is imprecise: not having a main method simply means that class cannot be used for launching the application. You will only see an exception if you try to use that particular class to launch an application
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!