• 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

Java Constructors

 
Greenhorn
Posts: 24
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, Somebody please help me understand what's the actual purpose of having a constructor in java, as per my understanding the purpose of constructors is not to construct objects but to initialize/set values to the class variables, objects are created in java using new operator, but if the only purpose of constructors is to initialize/set values for variables in the class, then that can be very well done using static initializers why have constructors then....

Thanks,
Shiv
 
Greenhorn
Posts: 8
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static block is executed before calling the constructor.

So.. it is based on your requirement how you want to initialize the properties.

In some cases you might want to give privilege to others to supply the property values and you can go with the constructor with arguments.

Hope this makes sense.
 
Greenhorn
Posts: 4
Google Web Toolkit Hibernate Eclipse IDE
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Static initializers ( I hope you mean static block ) can't initialize private instance variables, so you need constructors. Code in static initializers are executed just after class is loaded by JVM ( Class Loader to be specific). Code in constructors are executed just after a new instance is created in memory.
 
Shiv Vishwakarma
Greenhorn
Posts: 24
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Arvind and Ramesh, I agree with your comments but why at all have constructors (default construtors) when i do not have any instance variables to initialize.
 
Ramesh Kumar Muthukumar
Greenhorn
Posts: 8
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
that is why default constructor is not mandatory
 
Shiv Vishwakarma
Greenhorn
Posts: 24
Eclipse IDE MySQL Database Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
but it is still provided by the JVM
 
Ramesh Kumar Muthukumar
Greenhorn
Posts: 8
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
how do you create an instance for a class? - only through the 'new' keyword followed by the default constructor.

So when you do not have the default constructor the jvm provides one when creating the instance.
 
Bartender
Posts: 5167
11
Netbeans IDE Opera Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Ramesh Kumar Muthukumar wrote:how do you create an instance for a class? - only through the 'new' keyword followed by the default constructor.

So when you do not have the default constructor the jvm provides one when creating the instance.



No, the compiler provides one when you compile the class.

edit: As mandated by the JLS: http://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Shiv Vishwakarma wrote:but it is still provided by the JVM


Actually, as someone already said, by the compiler. And in my view, letting the compiler do things for you is a bad habit to get into, because it can lead to you getting compiler errors for things you can't see (the compiler puts the constructor in your .class file, not your source code).

So my advice is to always write your constructors (and calls to superclass constructors) explicitly - even if they're the same as the ones that would be created for you automatically.

As to initializers: they're needed very rarely. I can't remember the exact time I last wrote one, but it was certainly more than a year ago; and I write classes most days.

Winston
 
Create symphonies in seed and soil. For this tiny ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic