• 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:
  • Campbell Ritchie
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

object creation doubt

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi to all...
When i try to compile the following code the compiler gives the error
"illegal start of type"
 
Ranch Hand
Posts: 2412
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Notice the code the way you have it written.



You cannot place code like that inside a class definition but outside of a block.

If you place the code within a block, you won't get a compile-time error.



Note that this would also work.

 
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
"bhaskar", you have been asked here to change your display name to conform with our JavaRanch Naming Policy. Please change your display name immediately.

Your previous invalid display name was "bhaskar sir", do not use that.

Thanks.
 
Vaibhav Wahee
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
why do we need a code block to write new Main() in a class...
and this is fact is no where mentioned in K&B Book....
 
Ranch Hand
Posts: 3389
Mac MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Because, Class is just a Template -> Unit of description of its members.
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

I am preparing for SCJP too, and new to this forum. But I am really happy to find out point like this abt java.

But for this question I still not able to understand that why we need one additional block to create a main obj. Can someone also recommend some article ?

I am reffering to khalid mughal and I dont think I have come across a point like this mentioned above.

Thanks,
Anand
 
Ranch Hand
Posts: 74
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi

In java , you cannot create code like this.

class Main { new Main();}

we can only declare variable (instance and static variable)like this.
Main m = new Main();

and this is not any kind of declaration. Apart from this, you need to put your code inside method or block.so that it can be called.
 
Greenhorn
Posts: 28
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi,

Class is just a template to declare variable and methods. Any code other than this should go inside a block (say, static block, method body etc.).

Correct me if I'm wrong...
 
Your mind is under my control .... your will is now mine .... read this tiny ad
Garden Master Course kickstarter
https://coderanch.com/t/754577/Garden-Master-kickstarter
reply
    Bookmark Topic Watch Topic
  • New Topic