• 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

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

 
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I've a class Demo


OR


Which one do you prefer & why please ?
 
Ranch Hand
Posts: 954
4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Likes 3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
hahaha fred good example
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
ya got it Campbell & Tushar , thank you !
 
Ganish Patil
Ranch Hand
Posts: 529
19
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 8857
637
Mac OS X VI Editor BSD Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Bartender
Posts: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Eclipse IDE MySQL Database Tomcat Server Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
these kinds of exceptional cases in java gonna blow my mind, thank you Pawel
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
Saloon Keeper
Posts: 15510
363
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2236
63
IntelliJ IDE Firefox Browser Spring Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Careful not to confuse OP. Otherwise I shall move you to MD
 
fred rosenberger
lowercase baba
Posts: 13089
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
I'm a lumberjack and I'm okay, I sleep all night and work all day. Lumberjack ad:
a bit of art, as a gift, the permaculture playing cards
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic