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

really stupid question

 
Peg Garland
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I am working through the study guide for SCJP. On the first example I created a class:

public class Fruit {
System.out.println("Inside Fruit class");

}

It won't compile!
cannot find symbol: class out
location: java.lang.System.out

I imported java.lang.*; and that doesn't help. In the class with the main method, I can use System.out.println("Inside Main") with no imports. What is the difference?
 
marc weber
Sheriff
Posts: 11343
Java Mac Safari
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to JavaRanch!

Basically, Java classes contain fields and methods. Statements like...

System.out.println("Inside Fruit class.");

...must be inside a method body. (Or a constructor body, or an initialization block... But not by itself inside the class definition.)
 
Mark Vedder
Ranch Hand
Posts: 624
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You are calling a method outside a method, constructor or initializer block. You need to move your System.out.println() into one of those.
 
arulk pillai
Author
Ranch Hand
Posts: 3387
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In Java java.lang.*; is implictly imported. Are you using any Java IDE or using text pad?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That isn't a stupid question at all. But please, in future, tell us what your posts are about; see this FAQ.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by arulk pillai:
In Java java.lang.*; is implictly imported. Are you using any Java IDE or using text pad?
I think the compiler has imputed "java.lang.System" to "System" and is simply using its fully qualified name.
 
eshu khare
Ranch Hand
Posts: 44
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

the above code in asked doubt will compile witout 'MAIN'?
 
Campbell Ritchie
Sheriff
Pie
Posts: 49411
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have you tried it? Have you read the whole of this Thread? Have you ever had a class without a main (not MAIN) method?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic