1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

Simple Java Question About PACKAGES

Discussion in 'Programming' started by Kuraptka, Feb 4, 2006.

  1. #1
    Here's da deal:

    I have a directory named "dir". Inside this directory are 2 other directories: core1, and core2.
    I have a package named "core1". Inside this package is a file named "file1.java"
    I have another package named "core2". Inside this package is a file named "file2.java" and a corresponding class file "file2.class"

    Now...
    I want to import the "core2" package in file1.java so I can declare file2.
    How do I import the "core2" package?
    I tried
    import ../core2.* and many other combinations. I don't want to put in the full path, just relative paths. How do I do this?
     
    Kuraptka, Feb 4, 2006 IP
  2. khudania

    khudania Well-Known Member

    Messages:
    303
    Likes Received:
    9
    Best Answers:
    0
    Trophy Points:
    108
    #2
    Lets first make certain things clear

    You are in some directory 'dir'

    You have two java classes
    file1 which is core1.file1
    file2 which is core2.file2

    Now you want to make use of class file1 in file2

    Ok, there are three ways you can do this
    use the import statement like this import core1.file1
    Or
    use the import statement like this import core1.*
    Or use the complete class name where you are using file1 ie core1.file1


    Here is the skeleton code for both these


    package core1;
    public class file1
    {
    public static void main(String[] args)
    {
    System.out.println("Hello World!");
    }
    }



    package core2;
    import core1.*; //or u can use import core1.file1;
    public class file2
    {
    public static void main(String[] args)
    {
    core1.file1 = new core1.file1(); /* here we have used the complete class name directly and need not use import statement at all. */
    System.out.println("Hello World!");
    }
    }

    // place both the .java files in dir only and dont manually place them in packages core1 and core2, the command javac -d . *.java will place both in their respective package structure.

    and yes ../core2.* will not work since its not a url its a package structure.
     
    khudania, Feb 4, 2006 IP