Easy to Learn Java: Programming Articles, Examples and Tips

Start with Java in a few days with Java Lessons or Lectures

Home

Code Examples

Java Tools

More Java Tools!

Java Forum

All Java Tips

Books

Submit News
Search the site here...
Search...
 
Search the JavaFAQ.nu
1000 Java Tips ebook

1000 Java Tips - Click here for the high resolution copy!1000 Java Tips - Click here for the high resolution copy!

Java Screensaver, take it here

Free "1000 Java Tips" eBook is here! It is huge collection of big and small Java programming articles and tips. Please take your copy here.

Take your copy of free "Java Technology Screensaver"!.

Java Newsletters Archive: 19

JavaFAQ Home » Java Newsletters Go to all tips in Java Newsletters


Bookmark and Share

***********************************************
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* > The Java FAQ Daily Tips, weekly publication < *
* ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ *
* *
* Issue No: 19 10 January 2001 *
* http://www.javafaq.nu/java *
* *
* *
* Please recommend our FREE "100 Java Tips" book and us to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
***********************************************

Table of Contents

1. I propose that Java should allow multiple inheritance if...
2. I got a problem with an array/vector...
3. Does anyone know how to make a file NOT "read-only"???
It is not simple question...
4. I have a question about sending a reference to the object
via the socket...
5. Why my program does not give the address of the local
machine on one PC and give on another?
6. Can applet corrupt my registry file?
7. I'm converting an old java client/server program which
is based on raw byte stream...
***********************************************

Hello dear friends!

***********************************************

Tip 1 Q: I propose that Java should allow multiple inheritance if...
Everyone knows the potential problem with multiple inheritance is
when you run into the problem of having two instances of a grand
parent super class.

For example:
class A extends D {int i; }
class B extends D {int i; }
class C extends A,B {}

Potentially, you could have two copies of D for each instance of C.

However, I propose that Java should allow multiple inheritance if
there are no instance variables associated with the abstracts that
the base class is extending.

abstract class A {
public setX();
public setY();
public setAll() {setX (); setY();
}
abstract class B {
public setC();
public setD();
public setBoth(){setC(); setD(); }

class C extends A,B {}

You won't have two instances of some grandfather class, since A and
B doesn't have instances variables.

I hope the next versions of Java explores this issue.

Answer: It does. They're called interfaces:

interface A {
public void setX();
public void setY();
public void setAll();
}
interface B {
public void setC();
public void setD();
public void setBoth();
}

interface C extends A,B {};

public abstract class D implements C {
}

jim

***********************************************

Tip 2 Q: I got a problem with an array/vector...
I got a problem with an array/vector.

my class contains a member:
static Vector quad[][];
....

in my constructor I got:
Vector quad[][] = new Vector[row][col];
for (int i = 0; i < row; i++){
for (int j = 0; j < col; j++){
quad[i][j] = new Vector (0,1);
}
}

// row and col are int between (10..50) -- it's a big I know, but
that might not be the problem

My PROBLEM (and I don't know what to do, really), I can't access
quad[x][y] outside of the constructor!!!! Within the constructor
I've got full access on quad[x][x]. Java (1.2) returns a
NullPointerException on any method except within the constructor!!!

What's my fault!???

Answer: I guess you shouldn't write Vector here:
Vector quad[][] = new Vector[row][col];
so, the correct variant may be:
quad[][] = new Vector[row][col];

I guess You just overridden your static variable with one defined
in your constructor:
Vector quad[][].
Thus, you're initializing NOT your class-scope static variable
but your constructor-scope quad. It's not reachable outside the
constructor. And as for static quad, it has never been initialized!
And a first reference to it causes NullPointerException. I guess.
I hope I'm right Smile

***********************************************

Tip 3 Q: Does anyone know how to make a file NOT "read-only"???
Does anyone know how to make a file NOT "read-only"?? I know how
to make a File read-only, but I do not know how to do the opposite.
I have consulted the JavaDoc for File, FileDescriptor, FileSystem
and have not found a way of doing this. Any help would be greatly
appreciated.

To set Read-Only:
File file = new File( "c:/testFile.txt" );
file.setReadOnly();

A: You can't from Java. See:
http://developer.java.sun.com/developer/bugParade/bugs/4167472.html

I find Sun's argument rather weak however.

You can get around the problem by calling Runtime.exec() with a
command like "chmod" specific to your operating system, in order to
modify the file mode.

***********************************************

Tip 4 Q: I have a question about sending a reference to the object
via the socket...
I have a question about sending a reference to the object via the
socket. Two threads are communicating via sockets running on the
same machine. I don't need to send the whole object, but I need
to send just a reference.
Does anyone knows how to do that?

Answer: Reference to an Object? A reference is only valid within
the same memory space! If you want to be able to invoke methods
on an object remotely, then you will need to use a remote technology
like RMI, CORBA, or some such.

***********************************************

Tip 5 Q: Why my program does not give the address of the local
machine on one PC and give on another?

Answer: As long as you have TCP/IP installed, you should at
least get 127.0.0.1


***********************************************

Tip 7 Q: I'm converting an old java client/server program which
is based on raw byte stream heavily into new one which requires
utilizing object streams. But if I open input/output object
streams on both side this blocks system and won't proceed.


ObjectInputStream in = new ObjectInputStream(socket.getInputStream());
ObjectOutputStream out = new
ObjectOutputStream(socket.getOutputStream());

Answer: Upon opening the ObjectInputStream, the constructor blocks
to read a header from the stream (doing who-knows-what). Now, what
happens is, both your client and server open the InputStream... and
happily wait forever for the other side to send them the header
they want. Deadlock guaranteed!
The solution is simple: open the ObjectOutputStream first on at
least one side, but better on both sides (usually, symmetry == good).
Problem solved
Smile
You are trying to keep two streams going at once independently, right.
That means you need at least two threads at each end.

The Java FAQ Daily Tips is a newsletter that is only sent to those
who have specifically subscribed to it.

Copyright (c) 2000 John Andersson
***********************************************
* *
* You can find our tips on site also! *
* Please recommend us and our FREE "100 Java Tips" book to your *
* friends and colleagues! *
* http://javafaq.nu/java/advert/our_book.shtml *
* *
***********************************************


 Printer Friendly Page  Printer Friendly Page
 Send to a Friend  Send to a Friend

.. Bookmark and Share

Search here again if you need more info!
Custom Search



Home Code Examples Java Forum All Java Tips Books Submit News, Code... Search... Offshore Software Tech Doodling

RSS feed Java FAQ RSS feed Java FAQ News     

    RSS feed Java Forums RSS feed Java Forums

All logos and trademarks in this site are property of their respective owner. The comments are property of their posters, all the rest 1999-2006 by Java FAQs Daily Tips.

Interactive software released under GNU GPL, Code Credits, Privacy Policy