This week's book giveaway is in the JavaScript forum.
We're giving away four copies of Svelte and Sapper in Action and have Mark Volkmann on-line!
See this thread for details.
Win a copy of Svelte and Sapper in Action this week in the JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Bear Bibeault
  • Junilu Lacar
Sheriffs:
  • Jeanne Boyarsky
  • Tim Cooke
  • Henry Wong
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • salvin francis
  • Frits Walraven
Bartenders:
  • Scott Selikoff
  • Piet Souris
  • Carey Brown

object creation doubt

 
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • 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: 2410
  • Mark post as helpful
  • send pies
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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
  • 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...
    Bookmark Topic Watch Topic
  • New Topic