File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
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
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Calling constructor from another constructor Problem" Watch "Calling constructor from another constructor Problem" New topic

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: 446

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%], OCPWCD [84%], OCEJPAD [83%]
    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

    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.

    I agree. Here's the link:
    subject: Calling constructor from another constructor Problem
    It's not a secret anymore!