This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Calling constructor from another constructor Problem Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling constructor from another constructor Problem" Watch "Calling constructor from another constructor Problem" New topic
Author

Calling constructor from another constructor Problem

Mohit G Gupta
Ranch Hand

Joined: May 18, 2010
Posts: 634

self made




Kathy sierra pg134 last point ch-object orientation
the only way a constructor can be invoked is from within the another constructor

getting error:cannot find symbol max()

HOw this possible??


OCPJP 6.0 93%
OCPJWCD 5.0 98%
Minhaj Mehmood
Ranch Hand

Joined: Jan 22, 2007
Posts: 400

You're calling constructor max() without new keyword, change the code at like 9 as:



SCJP6 96% | SCWCD5 81% | SCDJWS5 79%
Seetharaman Venkatasamy
Ranch Hand

Joined: Jan 28, 2008
Posts: 5575

Constructor is a special kind of method. i.e, Constructor is not a method.



to invoke same class constructor use this()/ this used to invoke overloaded constructor . i.e,
Prasad Kharkar
Ranch Hand

Joined: Mar 07, 2010
Posts: 438

also I want to add some points..

  • We can call other constuctors by using overloaded verions of this
  • the first line in the construction should be implicit call to the super constructor or call to overloaded constructor using this
  • super() and this() cannot be between lines of code in constructor


  • SCJP 6 [86%] June 30th, 2010
    OCPWCD [84%] March 26th, 2013
    If you find any post useful, click the "plus one" sign on the right
    Shanky Sohar
    Ranch Hand

    Joined: Mar 17, 2010
    Posts: 1051

    Contructor cannot be called by their name as it already mentioned
    best way to call the overloaded constructor is through "this" keyword..


    SCJP6.0,My blog Ranchers from Delhi
    Mohd Abrar Khan
    Greenhorn

    Joined: Jan 20, 2010
    Posts: 18

    "This" is a keyword to refer current object properties and its location

    we can use it in two Cases
    1) To differentiate method and class Attributes

    2) To execute other constructor explicit statements.

    for your example it is to execute the no-arg constructor, hope i am correct.

    Thanks!
    Abrar
     
    jQuery in Action, 2nd edition
     
    subject: Calling constructor from another constructor Problem
     
    Similar Threads
    Constructors are not inherited.
    why not give a no-arg constructor
    Rules for Constructors - K&B Page 315
    Object Constructor "Challenge" / problem
    confusion with constructor