• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

Problem printing a string

 
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
 
Bartender
Posts: 7298
65
Eclipse IDE Firefox Browser MySQL Database VI Editor Java 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
 
Ranch Hand
Posts: 207
3
Oracle MySQL Database Java
  • 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.
 
Marshal
Posts: 70234
282
  • 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
 
    Bookmark Topic Watch Topic
  • New Topic