Results 1 to 4 of 4

Thread: packages

  1. #1

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    packages

    Why do you have to specify the package that a class is in if you are also forced to place it in the same directory structure? It seems redundant, and if you move the class to a different directory you also have to change it's package declaration.

    Is there a purpose for that design?
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  2. #2
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: packages

    It seems to be a misunderstanding on your part. The directory structure is used to quickly find the code for a class given its full name, not the other way round. If the directory structure was used to find out the package, you'd always have to give javac the base directory to work from. You could run into problems with case-insensitive systems like Windows. And there is no actual requirement for the source code to be in the directory structure - it's just convenient.
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

  3. #3

    Thread Starter
    Fanatic Member
    Join Date
    May 2001
    Posts
    837

    Re: packages

    Hmm, that's the first time your answer has made no sense to me.

    The directory structure is used to quickly find the code for a class given its full name, not the other way round.
    Ok, but why do you specify the package in the file where you define the class?

    there is no actual requirement for the source code to be in the directory structure
    I thought javac spit out errors if your class was not in the same directory structure as it's package declaration says it is?
    The human brain cannot hold all of the knowledge that exists in this world, but it can hold pointers to that knowledge.

  4. #4
    Kitten CornedBee's Avatar
    Join Date
    Aug 2001
    Location
    In a microchip!
    Posts
    11,594

    Re: packages

    No, it doesn't. It's only java that won't be able to find it. If given the -D option, however, javac will automatically put the .class files in their proper directories.

    You specify the package name for another reason: the source file should contain the entire information about the class; the file name and location should not affect the meaning of code. That's just good design practice, because the other way is too brittle. Compile the file anywhere else - in fact, simply with a slightly different compiler invocation -, and -- oops, class now has a different FQN and can't be loaded anymore. Sorry.

    Supposing this were my directory structure:
    Code:
    /home/wasti
      /projects
        /scm
          /release
            /src
              /at
                /getdesigned
                  /scm
                    /dao
                      FooDao.java
                      BarDao.java
                      FoobarDao.java
      /music
    What package do these three interfaces belong to? Well, depending on the command line, it could be any of these:
    Code:
    / $ javac ... # package home.wasti.projects.scm.devel.src.at.getdesigned.scm.dao
    ~ $ javac ... # package projects.scm.devel.src.at.getdesigned.scm.dao
    ~/projects/scm/devel $ javac ... # package src.at.getdesigned.scm.dao
    ~/projects/scm/devel/src $ javac ... # package at.getdesigned.scm.dao
    ~/projects/scm/devel/src/at/getdesigned $ javac ... # package scm.dao
    Bad - depending on from where I invoke the compiler, the code has different meanings. The absurdity of this is staggering.

    Or think about this scenario: the file is for download on a web page - how do you know which directory to correctly put it in? Say, it's an update for an application - "grab this file, put it in its directory and recompile".
    All the buzzt
    CornedBee

    "Writing specifications is like writing a novel. Writing code is like writing poetry."
    - Anonymous, published by Raymond Chen

    Don't PM me with your problems, I scan most of the forums daily. If you do PM me, I will not answer your question.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width