File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Error while executing the program Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Error while executing the program" Watch "Error while executing the program" New topic

Error while executing the program

Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486

Hello ranchers,

When i execute the above code

output : Exception in thread "main" java.lang.Error: Unresolved compilation problem:

at maindemo.main(

Can anyone explain me why its throwing an error.

But when i write System.out.println() in main class it prints .
Why is it so ???

Thanks<br />Dinesh
Ruben Soto
Ranch Hand

Joined: Dec 16, 2008
Posts: 1032
Hi Dinesh,

I think it might be illegal to put anything but declarations at the top level of a class. If you want that code to run everytime you instantiate an object of the class, you can put the System.out.println("hi"); code between curly braces, and that will convert it into an initialization block that will run everytime you create an instance of the class.

I rethought this, and you can also put initialization blocks, static initialization blocks, and method definitions (but a method definition is a type of declaration.)
[ December 17, 2008: Message edited by: Ruben Soto ]

All code in my posts, unless a source is explicitly mentioned, is my own.
Rajshekhar Paul
Ranch Hand

Joined: Oct 17, 2006
Posts: 140
Hello Dinesh,
You have written the statement

just inside the class without putting it in any blocks, constructors or methods.
It will fail the compilation. Thats why when you try to run the class, you are getting unresolved compilation error.

And what do you mean by main class?
If you tried to mean the main method, then it'll definitely print 'hi' as the statement is writtent inside a method.
[ December 17, 2008: Message edited by: Rajshekhar Paul ]

When it's obvious that you have to do it, just do it without shattering your thoughts over different directions.
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Can you guys please shed some lights on your replies. I am not able to get.
Ankit Garg

Joined: Aug 03, 2008
Posts: 9465

The line that I have made bold is creating the problem. If you want that line to be executed when an instance of demo or maindemo is created, then you must put that into braces to make it an initializer block

SCJP 6 | SCWCD 5 | Javaranch SCJP FAQ | SCWCD Links
James Tharakan
Ranch Hand

Joined: Aug 29, 2008
Posts: 580

@ Dinesh Tahiliani,
I think the other rancher has explained the issue.
If you still did not understand, then i think we will be able to explain your doubt if you can tell us why you wrote that print statement in that class ( it like a no-man's land).

Why to worry about things in which we dont have control, Why to worry about things in which we have control ! !
Dinesh Tahiliani
Ranch Hand

Joined: Aug 06, 2007
Posts: 486
Thanks anyways for explaining.
The question was asked in Interview
I agree. Here's the link:
subject: Error while executing the program
It's not a secret anymore!