Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

System.out.println not working in class

 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 5912
66
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you didn't call it in a method, you are calling it in a class body, which is illegal.
 
Greg Brannon
Bartender
Posts: 563
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another tip: Include all errors you're getting in your post.
 
Bud Tippins
Ranch Hand
Posts: 52
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thank you for your help. That worked.
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic