Re: Why a simple package statement

From: John C. Bollinger (jobollin_at_indiana.edu)
Date: 11/21/03


Date: Fri, 21 Nov 2003 13:06:52 -0500

lonelyplanet999 wrote:
> That means, if I should give a .java file a package name such that
> it's class can be referenced in other .java files. I must create
> corresponding directory to accommodate it ?

That is not absolutely required, but it is very good practice. Among
other things, it allows the java compiler to find and compile
dependencies at need.

> e.g. Now I place all .java files under c:\javapgm. I call
> c:\j2sdk1.4.1_01\bin\javac & c:\j2sdk1.4.1_01\bin\java under
> c:\javapgm to compile & run java programs.
>
> 1. If I named File1.java as "package package1;", I need to place
> File1.java under c:\javapgm\package1\

That should work. There are alternatives, but I wouldn't worry about
them right now.

> 2. If another file under c:\javapgm named File2.java without package
> statement needs to reference class inside package1.
> 3. File2.java needs the statement "import package1.*;" or
> "package1.<specific class>;" to import specific class.

The location of the source file is not directly relevant to how the Java
class(es) it describes must refer to other classes. If classes in any
package other than package1 want to access members of package1, though,
then the alternatives you describe are indeed the available options.

> 4. If another file named File3.java with "package package3;" statement
> needs to reference class inside File1.java too.
> 5. File3.java needs to be stored under "c:\javapgm\package3\".

This would be best.

> 6. File3.java needs import "package1.*" or "package.<specific class>;"
> inside it under "package package3;" statement.

As described above, the relevant criterion is the package to which a
class is assigned, not the structure of the source tree, but yes, that's
correct.

John Bollinger
jobollin@indiana.edu



Relevant Pages

  • Re: Java problem
    ... give me done with no prob, but there is no java file in /usr/bin/, ... Having everyone do things their own way is your idea of stabilizing? ... Who would it have hurt for either RH to make a system where Sun's packaging would drop in and run or for Sun to package it to match RH's arbitrary locations? ...
    (Fedora)
  • Re: NetBeans and JCreator
    ... I've been using 'default package' sine I thought it default to NetBeans. ... All of the compile and run command are grayed out. ... to the .java file, and where to put the compiled bytecode. ...
    (comp.lang.java.programmer)
  • Re: How to start compiling a big package directory tree ?
    ... which are located in different sub directories according to their package name. ... Unfortunately no further instructions (e.g. Ant script) are available. ... For example I found a "main" method in java file SomeGUI.java ... Search for all you need and compile all necessary classes in the sequence you need". ...
    (comp.lang.java.help)
  • Why MUST a JSP bean in a package
    ... After a lot of debugging I realized that if using Tomcat, a JSP bean ... must always be in a package. ... public class myBean ... because HAVING to put a JAVA file (a bean, after all, is just a JAVA ...
    (comp.lang.java.beans)
  • RE: Duplicate name error after copying task/ VS_ISCORRUPT
    ... What you have to do is copy the assemblies that you want to reference ... Dim oApp As New Application, ... Dim oExec As Executable ... ' Get package path ...
    (microsoft.public.sqlserver.dts)