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

Error while executing the program

 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Hello ranchers,

When i execute the above code

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

at maindemo.main(maindemo.java:10)


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 ???
 
Ruben Soto
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Rajshekhar Paul
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Can you guys please shed some lights on your replies. I am not able to get.
Pleaseeee........
 
Ankit Garg
Sheriff
Posts: 9521
22
Android Google Web Toolkit Hibernate IntelliJ IDE Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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

 
James Tharakan
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ 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).
 
Dinesh Tahiliani
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks anyways for explaining.
The question was asked in Interview
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic