Re: getting used to Java - question about "style"
From: Dario (drinking coffee in the office…) (dario_at_despammed.com)
Date: 06/30/04
- Next message: alexis.davoux_at_free.fr: "Re: Opening a Java Window from a jsp page on the client side"
- Previous message: Chris Smith: "Re: How do I get String.split() to do what I want?"
- In reply to: glunk: "getting used to Java - question about "style""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
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
- Next message: alexis.davoux_at_free.fr: "Re: Opening a Java Window from a jsp page on the client side"
- Previous message: Chris Smith: "Re: How do I get String.split() to do what I want?"
- In reply to: glunk: "getting used to Java - question about "style""
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|