• 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 Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Problem printing a string

 
Ranch Hand
Posts: 686
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Saloon Keeper
Posts: 10732
86
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic