• Post Reply Bookmark Topic Watch Topic
  • New Topic

java object creation  RSS feed

 
mohammad shadab
Ranch Hand
Posts: 62
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
as we know that the object are constructed by the constructor..if we do not provide any constructor then compiler will give a default constructor and that will be empty.now my question is that if that constructor is empty then where is the code which is constructing the objects????
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The constructor doesn't really create the object. The JVM creates the object, and then calls the constructor to initialize the member variables of the new object.

If you don't initialize member variables, then are initialized with default values (depending on the type of the variable, for example: null, 0, false etc.).
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The idea of the constructor is to get the object into a “consistent” state, i.e. all its fields have values which are consistent with the class' invariants. If you write an empty constructor, you are implying that the class is in a “consistent” state already with all those fields set to their “default” values, as Jesper has told you. In my opinion, every field should be initialised in the constructor, even if you are initialising it to 0. The Java tutorials used to say that it is poor style to rely on the default values and you should actually write i = 0; in the constructor.
 
mohammad shadab
Ranch Hand
Posts: 62
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The idea of the constructor is to get the object into a “consistent” state, i.e. all its fields have values which are consistent with the class' invariants. If you write an empty constructor, you are implying that the class is in a “consistent” state already with all those fields set to their “default” values, as Jesper has told you. In my opinion, every field should be initialised in the constructor, even if you are initialising it to 0. The Java tutorials used to say that it is poor style to rely on the default values and you should actually write i = 0; in the constructor.






yeah its ok that default value be given to the instance variables but i want to know that where is that code which is doing this work(assignment of degault values)
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You used too many [quote] tags; I have removed some.
I don't know where that code it; you would have to look for OpenJDK and download it and inspect its code. It is done by the JVM, as Jesper told you earlier. The Java Language Specification describes what process is followed initialising an object, but it will be difficult to read, I am sure.
 
mohammad shadab
Ranch Hand
Posts: 62
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
if some one else have good knowledge about this please help me to solve this problem.....
 
Stuart A. Burkett
Ranch Hand
Posts: 679
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
mohammad shadab wrote:
if some one else have good knowledge about this please help me to solve this problem.....

Kudos on being able to read and understand the link Campbell posted in just two minutes. If that didn't answer your question, maybe the Java Virtual Machine specification will. Chapter 5 looks promising.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please only use [quote] tags for somebody else's text. I don't think you will find more “knowledge” than you have already been given.
 
mohammad shadab
Ranch Hand
Posts: 62
Eclipse IDE Oracle
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
let see....i think i will get....
 
Consider Paul's rocket mass heater.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!