27.04.2013 Views

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

330 Java Tips.pdf - FTP Server

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

General <strong>Java</strong> Questions I<br />

// row and col are int between (10..50) -- it's a big I know, but that<br />

might not be the problem<br />

My PROBLEM (and I don't know what to do, really), I can't access quad[x][y] outside<br />

of the constructor!!!! Within the constructor I've got full access on quad[x][x]. <strong>Java</strong><br />

(1.2) returns a NullPointerException on any method except within the constructor!!!<br />

What's my fault!???<br />

Answer: I guess you shouldn't write Vector here:<br />

Vector quad[][] = new Vector[row][col];<br />

so, the correct variant may be:<br />

quad[][] = new Vector[row][col];<br />

I guess You just overridden your static variable with one defined in your constructor:<br />

Vector quad[][].<br />

Thus, you're initializing NOT your class-scope static variable but your<br />

constructor-scope quad. It's not reachable outside the constructor. And as for static<br />

quad, it has never been initialized! And a first reference to it causes<br />

NullPointerException. I guess. I hope I'm right :)<br />

--<br />

Xao Rom<br />

Q: I propose that <strong>Java</strong> should allow multiple inheritance if...<br />

Everyone knows the potential problem with multiple inheritance is when you run into<br />

the problem of having two instances of a grand parent super class.<br />

For example:<br />

class A extends D {int i; }<br />

class B extends D {int i; }<br />

class C extends A,B {}<br />

Potentially, you could have two copies of D for each instance of C.<br />

However, I propose that <strong>Java</strong> should allow multiple inheritance if there are no<br />

instance variables associated with the abstracts that the base class is extending.<br />

abstract class A { public setX(); public setY(); public setAll() {setX (); setY(); }<br />

abstract class B { public setC(); public setD(); public setBoth()<br />

{setC(); setD(); }<br />

class C extends A,B {}<br />

You won't have two instances of some grandfather class, since A and B doesn't have<br />

instances variables.<br />

I hope the next versions of <strong>Java</strong> explores this issue.<br />

Answer: It does. They're called interfaces:<br />

interface A { public void setX(); public void setY(); public void setAll(); }<br />

interface B { public void setC(); public void setD(); public void setBoth(); }<br />

interface C extends A,B {};<br />

file:///F|/350_t/350_tips/general_java-I.htm (15 of 31) [2002-02-27 21:18:17]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!