• Post Reply Bookmark Topic Watch Topic
  • New Topic

Java Constructors  RSS feed

 
Shiv Vishwakarma
Greenhorn
Posts: 24
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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
 
Ramesh Kumar Muthukumar
Greenhorn
Posts: 8
Eclipse IDE Java Linux
  • Mark post as helpful
  • send pies
  • 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.
 
Arvind Sri
Greenhorn
Posts: 4
Eclipse IDE Google Web Toolkit Hibernate
  • Mark post as helpful
  • send pies
  • 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 Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • Quote
  • Report post to moderator
that is why default constructor is not mandatory
 
Shiv Vishwakarma
Greenhorn
Posts: 24
Eclipse IDE Java MySQL Database
  • Mark post as helpful
  • send pies
  • 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
  • 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.
 
Darryl Burke
Bartender
Posts: 5167
11
Java Netbeans IDE Opera
  • Mark post as helpful
  • send pies
  • 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
 
Winston Gutkowski
Bartender
Posts: 10575
66
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • 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
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!