This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes System.out.println not working in class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "System.out.println not working in class" Watch "System.out.println not working in class" New topic
Author

System.out.println not working in class

Bud Tippins
Ranch Hand

Joined: Jan 28, 2011
Posts: 52
In the code below, why am I getting an error in the System.out.println that is inside of class Apple? One inside of main runs fine. Thank you for your help.

Stephan van Hulst
Bartender

Joined: Sep 20, 2010
Posts: 3575
    
  14

Because you didn't call it in a method, you are calling it in a class body, which is illegal.
Greg Brannon
Bartender

Joined: Oct 24, 2010
Posts: 557
Another tip: Include all errors you're getting in your post.


Learning Java using Eclipse on OpenSUSE 11.2
Linux user#: 501795
Bud Tippins
Ranch Hand

Joined: Jan 28, 2011
Posts: 52
Thank you for your help. That worked.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

It will also work if you simply enclose the statement in braces...

This surprises some people. So as a learning opportunity, can a Greenhorn explain why this works? And what triggers that code to execute?


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: System.out.println not working in class
 
Similar Threads
how to run a java file after writing and saving
Access Modifier protected -- SCJP Study Guide page 36
how to access a class within a package?
What is this() method
method in subclass not seeing method in abstract superclass