views:

120

answers:

3

as weSuppose that I am creating a Java project with the following classes

  1. com.bharani.ClassOne
  2. com.bharani.ClassTwo
  3. com.bharani.helper.HelperOne
  4. com.bharani.helper.support.HelperTwo

with files put immediately under the folder 'src'

  1. src/ClassOne.java
  2. src/ClassTwo.java
  3. src/HelperOne.java
  4. src/HelperTwo.java

and compile them using the command

$ javac -d classes src/*.java (assuming that classes directory exists)

The compiler compiles these files and put the class files in appropriate sub-directories inside the 'classes' directory like this

  1. classes/com/bharani/ClassOne.class
  2. classes/com/bharani/ClassTwo.class
  3. classes/com/bharani/helper/HelperOne.class
  4. classes/com/bharani/helper/support/HelperTwo.class

Because the spec mandates that the classes should go inside appropriate directory structure. Fine.

My question is this: When I use an IDE such as Eclipse or NetBeans, they create the directory structure for the source code directory ('src' directory here) as well. Why is that? Is it mandatory? Or, is it just a convention?

Thanks.

+4  A: 

Mainly convention. It makes sense for the source to mirror the binary structure.

Also, if you have two classes with the same name (but in different packages), how would you store the source if not in different directories?

Keeping the source in just one folder is fine for small projects, but once you have a larger project (hundreds of classes), grouping the source into packages makes things far more manageable.

developmentalinsanity
+1  A: 

I thought it was mandatory, but your experience suggests otherwise. Either way, it's just common sense, right? Large projects have so many source files - why make life more complicated by having different structures for your source and your class files?

dty
+4  A: 
Is it mandatory? 

No

Or, is it just a convention?

Yes, to reflect your package structure in your source tree.

I always thought that Java's package is a little bit broken:

it seems to be hierachical, but it is not.

it is a simple (unique) prefix to define seperate plain namespaces.

PeterMmm
It is an often overlooked fact that Java packages are in fact not hierarchical. On the contrary, Scala packages are!
thSoft