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 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" Watch "system.out.println" New topic
Author

system.out.println

Ramakrishna Konanki
Greenhorn

Joined: Apr 18, 2008
Posts: 20
hi this is ramakrishna. i'm having doubt in very basic question

class a{
System.out.println("hello");
}

it wont compile why?
what is the reason.....

if i write static block or non-static block it is working fine

class a{
//static
{
System.out.println("Hello");
}
}
Jan Hoppmann
Ranch Hand

Joined: Jul 19, 2010
Posts: 142

This doesn't compile because you try to call a method from outside a method. This is only allowed inside methods (not classes) or in static blocks.


Life is full of choices. Sometimes you make the good ones, and sometimes you have to kill all the witnesses.
Ramakrishna Konanki
Greenhorn

Joined: Apr 18, 2008
Posts: 20
thank q very much
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Ramakrishna, Please http://faq.javaranch.com/java/Use Real Words

and when ever you post code, use code tag.
James Elsey
Ranch Hand

Joined: Dec 21, 2007
Posts: 228

You could also put that code into the main() method

Have a look at the Java tutorials on the official website, they should help you out : http://download.oracle.com/javase/tutorial/

Also, might be a good idea to update your profile to include your "firstname lastname", before the mods pounce ;)


Kind Regards, James. OCPJP 1.6 || My SCJP / OCJCP Study Notes
Interested in : SCJP, Google App Engine, Stripes, Android;|| My Bite-Size SCJP Study Blog
Shanky Sohar
Ranch Hand

Joined: Mar 17, 2010
Posts: 1051

Welcome to JavaRanch.

Please USECODETAGS while posting your queries


SCJP6.0,My blog Ranchers from Delhi
 
jQuery in Action, 2nd edition
 
subject: system.out.println
 
Similar Threads
Instance Initializer means what
somthing tricky
Reflection: To check whether static block is there or not!
Question on - System.out.println method (static Vs non-static)
Difference between static and not static intializer blocks