• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why should I use constructor if I can initialize an instance variable without constructor ?  RSS feed

 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If I've a class Demo


OR


Which one do you prefer & why please ?
 
Tushar Goel
Ranch Hand
Posts: 934
4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Constructors are not only used for variable initialization. Here is jls explanation about the same.

Intsance variables comes into the memory once object is created and object is created using constructors only. Once object is created then only it values are initialized. So, in
your first case, value will be initialized only after object is created. Secondly default value for int is 0 not 1.

In your second example, you are not creating constructor.

 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you do not write a constructor to initialise all fields, you are risking exposing your object whilst in an inconsistent state, and risking errors in your output.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why should you wear your seat belt when driving to the store when you can do it without wearing one?

Because it's safer.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hahaha fred good example
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
ya got it Campbell & Tushar , thank you !
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm quite skeptical about it.
object is created using constructors only
I really have no notion about it but I read somewhere we can create an object without invoking constructor.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:
object is created using constructors only
I really have no notion about it but I read somewhere we can create an object without invoking constructor.
Constructor is being automatically created by java compiler if user not defining one - always.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
@ Vilda but there are some exceptional cases where objects are created without invoking constructor neither implicitly nor explicitly. I think that is Deserialization and clone something like that, I haven't read about it yet so I'm not sure, but will peruse soon.
 
Liutauras Vilda
Sheriff
Posts: 4917
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ganish Patil wrote:@ Vilda but there are some exceptional cases where objects are created without invoking constructor neither implicitly nor explicitly. I think that is Deserialization and clone something like that, I haven't read about it yet so I'm not sure, but will peruse soon.

I see, you're digging quite deep. Lets see what well experienced guys can comment on this.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a code example that shows serialization and cloning do not call constructors.It prints:
Create instances using constructors
In constructor of A.
In constructor of B.
Create an instance using serialization
Same object? false
Create an instance using clone()
Same object? false

which shows that new objects are created but no constructors are called.
 
Ganish Patil
Ranch Hand
Posts: 529
19
Chrome Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
these kinds of exceptional cases in java gonna blow my mind, thank you Pawel
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You did call constructors:



Granted, you called them once each and were able to create two instances via the serialization and cloning process, but the constructor WAS called (to get the initial object you deserialize/clone).
 
Stephan van Hulst
Saloon Keeper
Posts: 7969
143
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If we're being pedantic, I could randomly generate data and try to deserialize it to an object without ever using a single new keyword.

One of my bigger issues with Java is the magic involved in cloning/deserializing. I think if these features are to be supported, it should be done explicitly through reflection, to make apparent the costs of working with them. It's far too easy for people to use these features without realizing what potentially dangerous behavior happens underneath.
 
Paweł Baczyński
Bartender
Posts: 2077
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred, what I meant was I have not called any constructor to create a2 and b2 instances.

Here you have some code where I do not call any constructor at all
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Careful not to confuse OP. Otherwise I shall move you to MD
 
fred rosenberger
lowercase baba
Bartender
Posts: 12563
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:Here you have some code where I do not call any constructor at all

are you sure?

I get it. I really do. I'm just being hyper-literal. SOMEWHERE, constructors are being called. I don't know, but I think there is even a constructor being called for your byte array bytes. You may not be calling it explicitly, but i'm reasonably sure one is called.
 
Campbell Ritchie
Marshal
Posts: 56530
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I told you. You have gone to meaningless drivel where you can call each other impossible to your hearts' content without confusing OP
 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!