Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Calling constructor from another constructor Problem

 
Mohit G Gupta
Ranch Hand
Posts: 634
Chrome Eclipse IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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??
 
Minhaj Mehmood
Ranch Hand
Posts: 400
Hibernate Java Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You're calling constructor max() without new keyword, change the code at like 9 as:

 
Seetharaman Venkatasamy
Ranch Hand
Posts: 5575
Eclipse IDE Java Windows XP
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 446
1
Eclipse IDE MySQL Database Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
  •  
    Shanky Sohar
    Ranch Hand
    Posts: 1051
    Eclipse IDE Firefox Browser
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Contructor cannot be called by their name as it already mentioned
    best way to call the overloaded constructor is through "this" keyword..
     
    Mohd Abrar Khan
    Greenhorn
    Posts: 18
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    "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
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic