• Post Reply Bookmark Topic Watch Topic
  • New Topic

compiling VS executing  RSS feed

 
sireesha vadlamani
Greenhorn
Posts: 20
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why can't i COMPILE the following program???
public class Sample
{
System.out.println("hi");
}
Even though there is no main() method we can compile a program but we can't execute it.. but the above program is not compiled.
Thanks.
 
Ulf Dittmer
Rancher
Posts: 42972
73
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

You can't put statements directly into the class - statements need to be part of a method or a constructor.

You can put declarations (like "int a = 5;") directly into a class, but not statements.
 
Rico Felix
Ranch Hand
Posts: 411
5
IntelliJ IDE Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
An addition to the above is that a class is a definition of a new type that you are introducing to the system... a type is made up of two parts which are data and operations... so that when defining a class you specify what data it represents using field declarations such as int value; and what operations can be performed on the type such as void printMessage() { System.out.println("hi"); }... NB. operations can only be described using a method definition where executable statements go inside it to determine what it does...
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!