• Post Reply Bookmark Topic Watch Topic
  • New Topic

Doubt regarding Object class constructor  RSS feed

 
Rohan Deshmkh
Ranch Hand
Posts: 127
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
A constructor generally is used to instialize the instance variable.
What does the constructor of class object do?
I tried to look here:
http://docs.oracle.com/javase/1.5.0/docs/api/java/lang/Object.html#Object()
but i couldn't find any information in it's constructor section.
It only written like

Constructor Detail:
Constructor
public Object()


As we know that the constructor of super class is invoked when we try to create an object of a class.
Lets's take a example here:

So should i assume that When line 1 is executed , the constructor of class object is called which does nothing.
I know that the object class constructor will be called, but i want to that what that constructor does?


Also i had to ask one more thing, i had created another thread but as it was in Less visited section of the forum, i didn't received any reply.
So please have a look at this also and if you know anything about this, please guide me.
Here is the link:
http://www.coderanch.com/t/596171/open-source/General-Questions-Http
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Rohan Deshmkh wrote:A constructor generally is used to instialize the instance variable.
What does the constructor of class object do?


Probably nothing, since Object has no state. Theoretically it could calculate and cache the Object's hashCode() and toString() values, but I rather doubt that it actually does.

As we know that the constructor of super class is invoked when we try to create an object of a class.
Lets's take a example here:

So should i assume that When line 1 is executed , the constructor of class object is called which does nothing.


you should definitely assume that it's called, because the JLS says that it is. You shouldn't make any assumptions about what it does, because there's nothing to base those assumptions on and you don't care what it does. (Although it's a reasonable educated guess that it does nothing.) All you care about is that by the time your class's constructor is invoked, Object's has already completed and the Object part of your new instance is in a valid initial state.

 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can create an Object object.You can do things with that object. Not many, admittedly, but if you find the code for HashSet, you find it repeatedly adds such an object to a map.

To find the code: unzip the src.zip file in your Java installation folder., and look in there. The folders match the package names, so look in the util folder inside the java folder.

Every object has a sort of “core” inherited from Object, so whenever you call a constructor, you need to go back and call the Object() constructor too. So that constructor is the most frequently‑used of all. If you go back to src.zip, you can find the Object constructor, which probably looks like thisBut if Object didn’t have that constructor, you could never call it and never create any other sorts of Object.
 
Campbell Ritchie
Marshal
Posts: 56581
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I see there is nothing printed against the constructor in the documentation. That means I was mistaken about the constructor: it doesn’t have the documentation comment I posted a few minutes ago.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!