• 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
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Constructers in java

 
Ranch Hand
Posts: 54
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi..
i have 2 programs such tht:this is the first program

the second program is:


when i compiled this program..the second program gives errors
but if i use a construct it doenst give.

can any1 tell me the reason as to y it didnt compile b4 n then later it complied.
is it necessary tht a class must havea default constructs?
thanks in advance
Smyle
 
Ranch Hand
Posts: 283
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

This is a class with code not in a method nor constructor, so no compile.


This code is in a method (happens to be a constructor) so compiles ok.
 
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by smyle khanna:
is it necessary tht a class must havea default constructs?
thanks in advance
Smyle[/QB]



It is not necessary for a class to have a explicitly declared default constructor. If you do not specify one, the compiler will generate a default constructor for you. For example, you could have the following code for your example:




Although there is no explicit constructor, the compile will generate one that looks like:

public Second() {};

The above code is an example of a static initializer that executes when the class itself loads before object instantiation (and thus, the constructor) occurs.
 
Ranch Hand
Posts: 884
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In your first program, you're not instantiating a new object at all! Unless you've a typo here...



Not



If you've the latter, I don't think the compiler will be nice to you
 
Ranch Hand
Posts: 164
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Smyle,

The compilation gives error because.. the code
"second a=new second;"
is not assosciated with any method or is static. If you do not want to put it in any method put it in a static block. this will associate it with the class and not any instance of that class.
put it like this
"
static{
second a=new second;"
}"

It will compile.
Happy compiling.
 
I'm just a poor boy, I need no sympathy, because I'm easy come, easy go, little high, little low, little ad
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic