explict consructor calling

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • sanjay123456
    New Member
    • Sep 2006
    • 125

    explict consructor calling

    Dear Friends ,

    in java the creation of object of class hello is following

    hello h = new hello();
    here hello() means call explictely constructor calling mechnism

    but in c++

    we write only helllo h;

    and object is created and implictely constructor is callling

    my question is this why java is call explictely constructor mechnism is accepted
    why we do not write only hello h; and after that implictely constructor is called

    what is meaning of explictely constructor mechnism in java ?
    why is required ?


    sanjay
  • r035198x
    MVP
    • Sep 2006
    • 13225

    #2
    Originally posted by sanjay123456
    Dear Friends ,



    in java the creation of object of class hello is following



    hello h = new hello();

    here hello() means call explictely constructor calling mechnism



    but in c++



    we write only helllo h;



    and object is created and implictely constructor is callling



    my question is this why java is call explictely constructor mechnism is accepted

    why we do not write only hello h; and after that implictely constructor is called



    what is meaning of explictely constructor mechnism in java ?

    why is required ?





    sanjay


    Because in Java you create objects only through the new keyword.

    Comment

    • dmjpro
      Top Contributor
      • Jan 2007
      • 2476

      #3
      i think u r from c++ background
      if u write hello h() means in c++ ............is a function declaration..
      h is a function which returns object type hello.
      in java there is no direct access of object only access to object reference that is implicit pointer
      in java hello h means a object reference with null value until and unless u use new the jvm does not give it the memory
      need any more help plz send me reply..
      i am online...

      Comment

      • bala1980
        New Member
        • Feb 2007
        • 3

        #4
        Originally posted by sanjay123456
        Dear Friends ,

        in java the creation of object of class hello is following

        hello h = new hello();
        here hello() means call explictely constructor calling mechnism

        but in c++

        we write only helllo h;

        and object is created and implictely constructor is callling

        my question is this why java is call explictely constructor mechnism is accepted
        why we do not write only hello h; and after that implictely constructor is called

        what is meaning of explictely constructor mechnism in java ?
        why is required ?


        sanjay


        Hi,

        In java if we gave as Hello h means it just create a reference and not an object. Only after using new only the object fo the class is created.

        Hello h; - > Reference
        Hello h = new Hello() - > Object

        Comment

        • dmjpro
          Top Contributor
          • Jan 2007
          • 2476

          #5
          okay...
          in second case the h holds the address of the object

          Comment

          Working...