Re: Rules for constructors

From: Tony Morris (_at_dibblegoATbigpond.com@)
Date: 03/19/04


Date: Fri, 19 Mar 2004 22:08:42 GMT


> b) never call non-final methods from your constructor
> (or else they will be overridden causing chaos as I have experienced
> today)

To be more precise, never call an overrideable method from a constructor
using a 'this' reference.
An overrideable method is defined as any method that is non-private,
non-static, non-final and a member of a non final class.
This is the cause of your "problem".

A trivia question that takes advantage of this behaviour to cause
"trickery":
http://www.xdweb.net/~dibblego/java/trivia/answers.html#q4

-- 
Tony Morris
(BInfTech, Cert 3 I.T., SCJP[1.4], SCJD)
Software Engineer
IBM Australia - Tivoli Security Software
(2003 VTR1000F)


Relevant Pages

  • Re: quick ZipFile question
    ... > ZipFile takes a filename or a File object in the constructor. ... A common misunderstanding. ... IBM Australia - Tivoli Security Software ...
    (comp.lang.java.programmer)
  • Re: quick ZipFile question
    ... > ZipFile takes a filename or a File object in the constructor. ... A common misunderstanding. ... IBM Australia - Tivoli Security Software ...
    (comp.lang.java.help)
  • Re: Class throws execption??
    ... > The extending class does ... > not have a constructor, since everything that needs to be done is done ... EVERY Java class in the entire universe has a contructor. ... IBM Australia - Tivoli Security Software ...
    (comp.lang.java.programmer)
  • Rules for constructors
    ... This is because the constructor of B calls super, and by ... the initialisation of the member variables in class B ... (or else they will be overridden causing chaos as I have experienced ... overridden methods should not access member variables that are ...
    (comp.lang.java.programmer)
  • Re: Rules for constructors
    ... "Alex Hunsley" wrote in message ... > b) never call non-final methods from your constructor ... > (or else they will be overridden causing chaos as I have experienced ... Tim Ward ...
    (comp.lang.java.programmer)