Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Constructor of the Object class  RSS feed

 
Tatiana Petrova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, everyone! I faced with a problem. I decided to check the implementation of the Object class but as I found out there's no constructor there. Could you explain me, please, how is it possible?
According to API, it must be there. So, I can't understand what happends at a low level when I try to create a new object.
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

When no constructor is defined, it has a default no-args constructor.

Henry
 
Tatiana Petrova
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
When no constructor is defined, it has a default no-args constructor.

Henry


Ok. Is it possible to find the code implementation of this statement?
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tatiana Petrova wrote:
Ok. Is it possible to find the code implementation of this statement?


Not sure what you are asking. Are you asking for an example of *not* creating any constructors? Or are you asking how to instantiate an Object with the default constructor?

For the first, how is it possible to show how to not do something?

For the second, the code would look something like this.



Henry
 
Stefan Evans
Bartender
Posts: 1836
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No code example exists per se.
Would a link to the Java Language Specification suffice?
 
Ganesh Patekar
Bartender
Posts: 696
23
Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tatiana Petrova wrote:
Henry Wong wrote:
When no constructor is defined, it has a default no-args constructor.

Henry


Ok. Is it possible to find the code implementation of this statement?
  • Yes you can make sure this by following example.
  • Here Test class with no constructor is defined.
  • Test.java

  • Compile Test.java so we get Test.class where implicitly default constructor with no arguments is created.
  • How to see .class contents? I used Cavaj decompiler tool for decompiling.
  • Here is what Test.class actually has after decompiling.

  • Test.class
  • Same thing happens implicitly, when you create an object of Object.
  • Hope this helps
     
    Ganesh Patekar
    Bartender
    Posts: 696
    23
    Eclipse IDE Hibernate Java jQuery MySQL Database Netbeans IDE Oracle Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
  • In above example access modifier of Test class is package-private so implicitly it creates default constructor with package-private access modifier.
  • Lets see second example of Test class having public access modifier.
  • In following example access modifier of Test class is public, so implicitly it creates default constructor of Test with public access modifier.

  • Test.java
  • After decompiling Test.class using Cavaj decompiler:
  • You can also see that using javap –c Test.java command.
  • First example shows:
  • C:\>javap -c Test.class   Compiled from "Test.java"
    public class Test {        
    public Test();     
    .......More information is there

  • Second example shows:
  • C:\>javap -c Test.class   Compiled from "Test.java"
    class Test {        
    Test();         
    .......More information is there

  • On 0th step of code It shows
  • // Method java/lang/Object."<init>": ()V
  • Where ()V means it invokes default non-argument constructor of Object class.


  • Lets see with another example of Test class.

  • Test.java
  • Now compile it, then
  • Execute command on command prompt like this javap –c Test.class then
  • We get information in that after public static void main(java.lang.String[]);
  • Code: 4 shows ---> invokespecial #3                  // Method "<init>": ()V
  • Where : ()V means invokes non-argument constructor of Test class
  • And code:18 shows ---> invokespecial #6 // Method "<init>": (IFLjava/lang/String;)V
  • Where (IFLjava/lang/String;)V means invokes constructor having I means int and F means float and Ljava/lang/String means String as parameters. i.e.

  •  
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!