Re: getting used to Java - question about "style"

From: Dario (drinking coffee in the office…) (dario_at_despammed.com)
Date: 06/30/04


Date: Wed, 30 Jun 2004 15:16:49 +0200

glunk wrote:

> I am working on my very first Java project. I have been familiar with OO
> design for a long time. But I have never put the real thing into practice
> (formerly a VB 6 programmer which only buys you encapsulation). My company
> is shifting away from VB / ASP solutions and bringing in Java and JSP. I am
> one of the ground breakers (God help me).
>
> I am reading a book that a collegue gave me called "The Elements of Java
> Style" by Vermeulen, Ambler, Bumgardner, Metz, Misfeldt, Shur and Thompsaon.
> (A lot of authors for a really skinny book.) Anyway, ONE OF THE RULES THAT
> THEY HAVE IS
>
> "Do not call nonfinal methods from within a constructor."
>
> They go on to explain
>
> "Subclasses may override nonfinal methods and Java will dispatch a call to
> such a mrethod according to the actual type of the constructed object -
> before executing the derived class constructors. This means when the
> constructor invokes the derived method, the derived class may be in an
> invalid state. To prevent this, call only final methods from the
> constructor."
>
> I cannot understand what this says or means. I know that a final method is
> one that cannot be overridden. I do not understand what this parapgraph is
> intending to warn me against doing or why. Can someone explain, preferably
> with an example?

class Final {
   public static void main(String[]args) {
     FinalSub fs = new FinalSub();
     fs.print();
   }
   int n;
   Final() {
     n=5;
     print();
   }
   void print() {
     System.out.println("n="+n);
   }
}
class FinalSub extends Final {
   int x = 8;
   FinalSub() {
     x=10;
   }
   void print() {
     super.print();
     System.out.println("x="+x);
   }
}

Say what the previous example must print.
Then compile and run the previous example and check it!

- Dario



Relevant Pages

  • getting used to Java - question about "style"
    ... I am working on my very first Java project. ... "Do not call nonfinal methods from within a constructor." ... "Subclasses may override nonfinal methods and Java will dispatch a call to ... before executing the derived class constructors. ...
    (comp.lang.java.programmer)
  • Re: Overriding Methods
    ... in a derived class, the overridden method is called in the base class." ... As for the remaining minor difference between C++ and Java, ... careful about what you do from a constructor. ...
    (comp.lang.java.programmer)
  • Re: 2 newbie questions
    ... > the volume of a fish tank and a third that ties everything together. ... You create a .java file. ... Then you modify this by asking it to create a constructor method and calling ... When you have a series of variables in some array, and you have an array for ...
    (comp.lang.java.help)
  • Re: New (as in days) to Java - question about "super()" method
    ... > Hi, I'm Don, and I'm a serious greenie when it comes to Java. ... learn Java because Object Oriented Programming is quite different from ... > guts out of the method involved (a constructor, I believe, is the ... inheritance: if I have a class and I want to add some extra ...
    (comp.lang.java.programmer)
  • Re: Implicit overloads, non static
    ... you've disputed whether it's a matter of implementation rather ... that's clearer - but I wasn't disputing that. ... > I find a lot of things hard in Java. ... I'm not sure I'd call that a copy constructor myself, ...
    (microsoft.public.dotnet.languages.csharp)