Java Questions

Wouldn't it be better to ask these questions on comp.lang.java.* ? Not that they are necessarily off-topic, but you probably get a better answer faster that way.

Q: Probably a stupid question, but what's a .bin file? When I try to download the Java SDK from Sun for Linux, it shows me a file with the .bin extension. Perhaps a better question is, how do I get a free, decent Java implementation for Linux with the least amount of effort? Thanks. -- SteveHowell

A: Extensions are just informative in *nix. .bin is trying to tell you the file is executable. chmod it to be executable and run it.


Q: I'm trying to make a MockClass?. Basically, I want to unit test a function that calls a class' static method inside the function, by writing a mock of that class, and passing it in as a parameter. I'm using a class called a PdfWriter?, which saves PDF files. My MockWriter? will be a sibling of PdfWriter?; they'll both share the same abstract superclass, DocWriter?. Before trying to change the code to allow a MockClass? to be passed, here's what it looks like:

 public void saveDoc() {
...
PdfWriter?.getInstance(doc, new FileOutputStream?(pdfName));
...
 }

And I'd like for it to look something like:

 public void saveDoc(class writerClass) {
...
((PdfWriter?) writerClass).getInstance(doc, new FileOutputStream?(pdfName));
...
 }

... but that doesn't work. So, I guess my question is, is there any way to downcast a class object so it returns a given subclass?

A: It ends up (I think) that Java static methods are bound at compile-time using the class name that you use in the source. They are unavailable directly from a class' Class object.

Reflection might be an option. Class objects have a getMethod(name, parameters) method. I would guess that it would return the correct getInstance Method object, and you could then do an invoke() on that. It is a bit awkward, but might be the thing to do... -- BenSchroeder


Q: I'm converting a pascal program to java. The pascal program starts by reading a text file full of integers and a few names. In pascal you can say read(x) and it will do an appropriate read given the type of x. I can't find anything close in java.io. What am I missing?

A: I think I don't understand the question, but I answer with a link: http://java.sun.com/people/linden/faq_b2.html#I/O Why doesn't it answer your question?


Q: Imagine you had a java.awt.TextArea packed within a java.awt.Frame. Now what would you do if you wanted the TextArea to be resized with the Frame? How would you set about doing that? (Obviously, you can listen for the ComponentEvent that fires when the Frame is resized... but how would you get the size of the TextArea right?)

A: You can use a LayoutManager object to handle that kind of thing for you. Each LayoutManager has a particular policy about how it lays out things in a given Container. It is asked to do the laying out automatically when frame resizes happen.

The Java Tutorial at Sun has a page with an overview at "http://java.sun.com/docs/books/tutorial/uiswing/overview/layout.html". There's a good page (which was mentioned in the Java FAQ on the subject) on layouts at "http://www.softbear.com/people/larry/javalm.htm".

That's not the answer - that's just saying "read up on layout managers" - done. Still I don't know how to make a resizable TextArea. It's silly when you resize a Frame, and your TextArea still has the same number of text columns and rows.

Sorry -- wasn't sure what was going on. Are you doing something like

 Frame frame = new Frame();

frame.setLayout(new GridLayout(1, 1)); frame.add(new TextArea());

frame.pack(); frame.setVisible(true);

and the TextArea stays the same size when you resize the frame?

I did just try this with a FlowLayout instead of the GridLayout. It doesn't resize the TextArea; it just centers it at the top of the frame. (That's what it is supposed to do, though; it just might not be expected.) Other layouts may do similar things.


Q. I have an abstract class and I want to declare an abstract constructor, so that if I later create a subclass without that particular constructor, I'll get a compile-time error to remind me. (I'd like this reminder 'cause I'm thinking of doing some gnarly stuff with reflection, so I don't get the normal compile-time checking if the constructor doesn't exist.) Is there a way to do this?

A. Couldn't you just declare a constructor (with appropriate parameters) that does nothing ? This would force subclasses to call the constructor from their constructor (unless it's a default constructor). --JeanPhilippeBelanger

That would work, but most of the subclasses have multiple constructors, and only some of them are related to this abstract constructor I'd like. (Again, it's gnarly stuff.) Having all of the multiple constructors call the superclass' constructors seems like it would make things more gnarly, not less.

A. What you want is to get the compiler to give you a warning. The compiler will warn if you don't call ANY constructor (so Jean Philippe's suggestion would help ensure that your sub-class constructors called SOME superclass constructors), but there's no way to get the compiler to warn you if you fail to provide a constructor with a certain signature. It sounds to me (correct me if I'm wrong) as if you want a constraint of the following form: "All subclasses of this class MUST provide a constructor which takes a File and a String" (or whatever your constructor requries). This is a useful sort of constraint, but Java provides no means within the language to specify it, so there's no way to get your compiler to warn you.

Fortunately, UnitTests come to the rescue. Instead of depending on the compiler, write a UnitTest to test whether the desired constructor is present, then make that part of a suite of tests run on ALL subclasses of your parent class.

Or, if I've misunderstood the problem, please let me know and I'll try to revise the answer! -- MichaelChermside

No, you've described the problem exactly, thanks. I would use some reflection-based testing to ensure it (I'm already using JavaUnit), except I don't think there's a way to find all of a class' subclasses. Short of writing Java that actually reads the source code, looking for the string "extends <superclass>". Which I may end up doing, when I get around to it.

I actually find it easier to read the .class file instead of the source file for this sort of thing; the .class file is in a nice strict format and the fully-qualified name of the superclass and the signatures for all the constructors are easily extracted from the constant pool. -- AlexPopiel

The ElipseIde? and VisualAgeJava have mechanisms to find the subclasses of a class. I imagine that others do, too. Of course, your tests won't function outside of the environment, but I doubt that would be much of a problem.


CategoryJava


EditText of this page (last edited February 24, 2003) or FindPage with title or text search