Java compilation problem - Caused by package access

Discussion in 'Java' started by JSR, Dec 10, 2003.

  1. JSR

    JSR Guest

    If class TestPackage and DefPack are in 2 separate files, how can we
    access DefPack from TestPackage?

    Note -
    1. TestPackage is in package testapps and DefPack is in the default
    package space. This is the problem.
    2. If I were to comment out the package statement within
    TestPackage.java, this example would compile.
    3. I need to be able to compile with the pacakge specifications being
    the way they are.

    // Compilation unit - TestPackage.java

    package testapps;

    public class TestPackage {
    public static void main(String[] args) {
    new DefPack();
    }
    }


    // Compilation unit - DefPack.java

    public class DefPack {
    public DefPack() {
    System.out.println("DefPack created");
    }
    }
    JSR, Dec 10, 2003
    #1
    1. Advertising

  2. JSR wrote:

    > If class TestPackage and DefPack are in 2 separate files, how can we
    > access DefPack from TestPackage?
    >
    > Note -
    > 1. TestPackage is in package testapps and DefPack is in the default
    > package space. This is the problem.
    > 2. If I were to comment out the package statement within
    > TestPackage.java, this example would compile.
    > 3. I need to be able to compile with the pacakge specifications being
    > the way they are.


    You really can't do that. You can not import classes from the default (== no
    package statement given) package. So you'll never be able to reach DefPack
    from TestPackage.

    The only solution is to put DefPack in a proper package.

    >
    > // Compilation unit - TestPackage.java
    >
    > package testapps;
    >
    > public class TestPackage {
    > public static void main(String[] args) {
    > new DefPack();
    > }
    > }
    >
    >
    > // Compilation unit - DefPack.java
    >
    > public class DefPack {
    > public DefPack() {
    > System.out.println("DefPack created");
    > }
    > }


    --
    Kind regards,
    Christophe Vanfleteren
    Christophe Vanfleteren, Dec 11, 2003
    #2
    1. Advertising

  3. JSR

    JSR Guest

    Christophe Vanfleteren <> wrote in message news:<1tOBb.71530$-ops.be>...
    > JSR wrote:
    >
    > > If class TestPackage and DefPack are in 2 separate files, how can we
    > > access DefPack from TestPackage?
    > >
    > > Note -
    > > 1. TestPackage is in package testapps and DefPack is in the default
    > > package space. This is the problem.
    > > 2. If I were to comment out the package statement within
    > > TestPackage.java, this example would compile.
    > > 3. I need to be able to compile with the pacakge specifications being
    > > the way they are.

    >
    > You really can't do that. You can not import classes from the default (== no
    > package statement given) package. So you'll never be able to reach DefPack
    > from TestPackage.
    >
    > The only solution is to put DefPack in a proper package.
    >
    > >
    > > // Compilation unit - TestPackage.java
    > >
    > > package testapps;
    > >
    > > public class TestPackage {
    > > public static void main(String[] args) {
    > > new DefPack();
    > > }
    > > }
    > >
    > >
    > > // Compilation unit - DefPack.java
    > >
    > > public class DefPack {
    > > public DefPack() {
    > > System.out.println("DefPack created");
    > > }
    > > }



    Thanks for the info. I unfortunately have to integrate my code with
    some third party code written this way.
    JSR, Dec 11, 2003
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.

Share This Page