• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Error while executing the program

 
Ranch Hand
Posts: 486
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ???
 
Ranch Hand
Posts: 1032
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
Ranch Hand
Posts: 140
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Can you guys please shed some lights on your replies. I am not able to get.
Pleaseeee........
 
Sheriff
Posts: 9708
43
Android Google Web Toolkit Hibernate IntelliJ IDE Spring Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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

 
Ranch Hand
Posts: 580
Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks anyways for explaining.
The question was asked in Interview
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic