Two Laptop Bag
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
Register / Login


JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply 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
    If you find any post useful, click the "plus one" sign on the right
    Shanky Sohar
    Ranch Hand

    Joined: Mar 17, 2010
    Posts: 1047

    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
     
    I agree. Here's the link: http://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to run our stuff on 16 servers instead of 3.
     
    subject: Calling constructor from another constructor Problem
     
    Similar Threads
    confusion with constructor
    Rules for Constructors - K&B Page 315
    Constructors are not inherited.
    Object Constructor "Challenge" / problem
    why not give a no-arg constructor