JavaNamespacesForIdiots

ThoughtStorms Wiki

Here's why I hate the fucking JavaLanguage soooo much.

And it might be a useful example for others.

I haven't worked with Java for a couple of years. And I figured I might have got over my pathalogical hatred and nausea I feel whenever I have to look at it. And anyway, I'm teaching it tonight.

So I boot up an IDE and have my copy of "Java in a Nutshell" available for syntax reference. I write and compile and run my HelloTeenageAmerica program. Now I bang out my standard OO example.

I open a file called Coord2D and write :

public class Coord2D { private int x; private int y;

public Coord2D() { this.x = 0; this.y = 0; }

public void set(int x, int y) { this.x = x; this.y = y; }

public String paraString() { String s; s = "(" + this.x + "," + this.y + ")"; return s; } }

// and a "main" file

import Coord2D; public class Main { public static void main(String[] args) { System.out.println("hello teenage america\n");

 Coord2D c;
 Coord2D c2;
 c = new Coord2D();
 c2 = new Coord2D();
 System.out.println(c.paraString());
 System.out.println(c2.paraString());
}}

All more or less intuitive.

I hit compile. And, of course, it blows up on line 1.

Main.Java:1: '.' expected

import Coord2D;

OK. I know what this error is ... sort of ...

The problem is that it isn't enough to just put a file in the same directory (and therefore namespace) and try to import it. Java wants some kind of qualified name. But I haven't put the file in a subdirectory. I left it in the default "package" (ie. namespace). So how should I fix this?

The OReilly Nutshell book is no help. It has a section on importing things from libraries. It reminds me of other restriction that piss me off about Java : that you can only have one public class per file, and that the files have to live in a directory structure that echoes the package space structure. But it doesn't tell me what I should do in the simple case where I want to start a simple application with a couple of files in the same directory.

So I hit the internet. This is a basic, beginners problem. I have it because I'm basically rusty and forgot how I solved it last time. But no "learn Java in 5 minutes for retards" site is going to even mention this difficulty. Sun's "Solving Common Compiler and Interpreter Problems" (doesn't http://java.sun.com/docs/books/tutorial/getStarted/problems/index.html) doesn't) mention it.

Then, of course, I get an idea. Perhaps I don't need to write anything to explicitly import the Coord2D. I simply take out the offending "import" line and, lo and behold, the code works.

I've just wasted an hour and a half of my time. I feel like a total idiot. I couldn't find any solution online because it's a non problem. And ...

I totally blame Java

for making me feel bad about myself!!!

bah!!!

PythonLanguage never puts me down or makes me feel inadequate.

8-(

Backlinks (1 items)