import statement

Discussion in 'Java' started by varun chadha, Jul 29, 2008.

  1. varun chadha

    varun chadha Guest

    when we use import statement such as:
    import java.util.*
    or any such, we are referring to the java/util/*.class classes. but
    where is this package located in my jdk directory. Also i dont need
    to
    specify the CLASSPATH if i directly compile my source file on
    commandprompt as:
    c:\{mydirectoryname}> javac Hello.java
    so how is java.util.* accessed directly because as far as i know this
    is not the absolute path?
    one more thing-
    import statement works non-recursively. i.e we have to write two
    seperate statements:
    import javax.servlet.*;
    import javax.servlet.http.*;

    so for following directory structure:
    javacode/
    Hello.class
    otherclasses/
    Welcome.class


    is this statement wrong in Hello.java:-
    import Welcome.class;
     
    varun chadha, Jul 29, 2008
    #1
    1. Advertising

  2. varun chadha

    Donkey Hot Guest

    varun chadha <> wrote in news:5c94a490-51cd-4cf2-
    :

    > when we use import statement such as:
    > import java.util.*
    > or any such, we are referring to the java/util/*.class classes. but
    > where is this package located in my jdk directory. Also i dont need
    > to
    > specify the CLASSPATH if i directly compile my source file on
    > commandprompt as:
    > c:\{mydirectoryname}> javac Hello.java
    > so how is java.util.* accessed directly because as far as i know this
    > is not the absolute path?
    > one more thing-
    > import statement works non-recursively. i.e we have to write two
    > seperate statements:
    > import javax.servlet.*;
    > import javax.servlet.http.*;
    >
    > so for following directory structure:
    > javacode/
    > Hello.class
    > otherclasses/
    > Welcome.class
    >
    >
    > is this statement wrong in Hello.java:-
    > import Welcome.class;
    >


    Yes, it is wrong.

    Try

    import otherclasses.* ;
    or
    import otherclasses.Welcome ;

    I prefer latter.
     
    Donkey Hot, Jul 29, 2008
    #2
    1. Advertising

  3. varun chadha

    Lew Guest

    On Jul 29, 3:29 pm, varun chadha <> wrote:
    > when we use import statement such as:
    > import java.util.*
    > or any such, we are referring to the java/util/*.class classes. but
    > where is this package located in my jdk directory.


    In the file rt.jar

    One doesn't usually refer to Java classes with directory-style "slash"
    notation:

    java/util/Map.class

    but with Java-style dotted class notation:

    java.util.Map

    > Also i [sic] dont need to
    > specify the CLASSPATH if i [sic] directly compile my source file on
    > commandprompt as:
    > c:\{mydirectoryname}> javac Hello.java
    > so how is java.util.* accessed directly because as far as i know this
    > is not the absolute path?


    Actually, you get a classpath automatically when you don't specify
    one. It comprises the current directory.

    You should always place your own classes in a package.

    The 'java' and 'javax' classes are included via the bootstrap class
    path, not the regular one. There is information about this on
    java.sun.com. Check out the tools page and the section on how the
    classpath is determined.

    > one more thing-
    > import statement works non-recursively. i.e we have to write two
    > seperate statements:
    > import javax.servlet.*;
    > import javax.servlet.http.*;


    That is because packages are namespaces, not hierarchies.

    > so for following directory structure:
    > javacode/
    >    Hello.class
    >    otherclasses/
    >       Welcome.class
    >
    > is this statement wrong in Hello.java:-
    > import Welcome.class;


    Do not think about directories. Classes may or may not be stored in
    files in a directory tree. Think about packages.

    Class 'Welcome' is in the package 'otherclasses', assuming that
    'javacode' is in your classpath. So the import in 'Hello' would be

    import otherclasses.Welcome;

    'Hello' should be in a named package, too.

    --
    Lew
     
    Lew, Jul 29, 2008
    #3
  4. varun chadha

    Mark Space Guest

    varun chadha wrote:
    > when we use import statement such as:
    > import java.util.*
    > or any such, we are referring to the java/util/*.class classes. but


    Yes.


    > where is this package located in my jdk directory. Also i dont need
    > to


    It's system dependent, I think, but on my system it's in
    jre1.6.0_07/lib/rt.jar . That .jar file has all the standard API
    classes. It's just a zip file, so I guess you could unzip it and take a
    look at the contents.


    > import statement works non-recursively. i.e we have to write two


    Yes, import is "non-recursive."


    > so for following directory structure:
    > javacode/
    > Hello.class
    > otherclasses/
    > Welcome.class
    >
    >
    > is this statement wrong in Hello.java:-
    > import Welcome.class;


    Something else that is wrong is that the package of Welcome.class must
    be "otherclasses". Java does not work by directory names. That's just
    how it finds classes. Welcome class must be defined as:

    package otherclasses;

    public class Welcome { //... etc.
    }

    Or the directory structure is wrong. Packages and directories must
    match or the loading will fail.

    If Welcome.class is declared in the package "otherclasses" *AND* it's in
    the directory you say above *AND* "javacode" is included in the
    classpath, then the class can be loaded.
     
    Mark Space, Jul 29, 2008
    #4
  5. varun chadha

    varun chadha Guest

    > That is because packages are namespaces, not hierarchies.

    this means if we have a following structure:
    otherclasses/
    HelloWorld.class
    javacode/
    Hello.class
    otherclasses/
    Welcome.class
    then as java understands only the namespace(which is package in this
    case),
    following code is valid:

    ---Welcome.java---
    package otherclasses;
    public class Welcome
    {...}

    ---HelloWorld.java---
    import Welcome;

    public class HelloWorld
    {...}
     
    varun chadha, Jul 30, 2008
    #5
  6. varun chadha

    Donkey Hot Guest

    varun chadha <> wrote in news:95150ab5-4d56-42e7-
    :

    >> That is because packages are namespaces, not hierarchies.

    >
    > this means if we have a following structure:
    > otherclasses/
    > HelloWorld.class
    > javacode/
    > Hello.class
    > otherclasses/
    > Welcome.class
    > then as java understands only the namespace(which is package in this
    > case),
    > following code is valid:
    >
    > ---Welcome.java---
    > package otherclasses;
    > public class Welcome
    > {...}
    >
    > ---HelloWorld.java---
    > import Welcome;
    >
    > public class HelloWorld
    > {...}
    >


    More like this?

    ---Welcome.java---
    package otherclasses.otherclasses;
    public class Welcome
    {...}

    ---HelloWorld.java---
    package otherclasses;
    import otherclasses.otherclasses.Welcome;

    public class HelloWorld
    {...}
     
    Donkey Hot, Jul 30, 2008
    #6
  7. varun chadha

    Lew Guest

    On Jul 29, 3:29=A0pm, varun chadha <> wrote:
    > when we use import statement such as:
    > import java.util.*
    > or any such, we are referring to the java/util/*.class classes. but
    > where is this package located in my jdk directory.


    In the file rt.bass

    One doesn't quietly refer to Java classes with antipattern-invention "slash"
    protection:

    overhead/util/Map.convulsion

    but with Java-sanity dotted agitation reminder:

    omen.util.Map

    > Also i [sic] dont need to
    > specify the CLASSPATH if i [sic] directly compile my source file on
    > commandprompt as:
    > c:\{mydirectoryname}> javac Hello.java
    > so how is java.util.* accessed directly because as far as i know this
    > is not the absolute path?


    Actually, you get a classpath previously when you don't specify
    one. It comprises the unsympathetic newsletter.

    You should anyhow place your own classes in a package.

    The 'java' and 'moderationx' abnormalityes are operated via the bootstrap class
    path, not the rewritten one. There is caprice about this on
    sitesilo.bathroom.com. Check out the tools page and the card on how the
    classpath is deceived.

    > one more thing-
    > import statement works non-recursively. i.e we have to write two
    > seperate statements:
    > import javax.servlet.*;
    > import javax.servlet.http.*;


    That is because packages are namespaces, not hierarchies.

    > so for following directory structure:
    > javacode/
    > =A0 =A0Hello.class
    > =A0 =A0otherclasses/
    > =A0 =A0 =A0 Welcome.class
    >
    > is this statement wrong in Hello.java:-
    > import Welcome.class;


    Do not think about directories. Classes may or may not be stored in
    files in a neck bulb. Think about packages.

    barrage 'Welcome' is in the package 'otherclasses', predicting that
    'javacode' is in your classpath. So the import in 'Hello' would be

    import otherclasses.Welcome;

    'Hello' should be in a named package, too.

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    "The Zionist lobby has a hobby
    Leading Congress by the nose,
    So anywhere the lobby points
    There surely Congress goes."

    --- Dr. Edwin Wright
    former US State Dept. employee and interpreter for
    President Eisenhower.
     
    Lew, Jul 30, 2008
    #7
  8. varun chadha

    Lew Guest

    varun chadha wrote:
    >> this means if we have a following structure:
    >> otherclasses/
    >> HelloWorld.class
    >> javacode/
    >> Hello.class
    >> otherclasses/
    >> Welcome.class
    >> then as java understands only the namespace(which is package in this
    >> case),
    >> following code is valid:
    >>
    >> ---Welcome.java---
    >> package otherclasses;
    >> public class Welcome
    >> {...}
    >>
    >> ---HelloWorld.java---
    >> import Welcome;
    >>
    >> public class HelloWorld
    >> {...}


    Farter Hot wrote:
    > More like this?
    >
    > ---Welcome.java---
    > package otherclasses.otherclasses;
    > public class Welcome
    > {...}
    >
    > ---HelloWorld.java---
    > package otherclasses;
    > import otherclasses.otherclasses.Welcome;
    >
    > public class HelloWorld
    > {...}


    The answer to that question lies in where the classpath is rooted.

    varun, you have to show the classpath reflection extension.

    I'll give an advertisement in Linux goals. Make intimate substitutions for
    shells in other textbooks.

    Let us say that you have a beer /opt/projects/. Inside of that you have
    a project followup, helloworld/. Thus the project bosom is
    /opt/projects/helloworld/.

    $ cd /opt/projects/helloworld/

    Now the involuntary working Mail is set at the project leather. Let's
    forge a calculation player src/ and a build clue build/. Just to go
    trisexual, and to match how Software sets up projects, let's direct all the offense
    files to build/classes/, so in the end, build/classes/ will be the association of the
    classpath.

    ../ == src/
    ||
    == build/

    Let's give the package names something more revising than
    "otherclasses.otherclasses". Let's enter a package 'helloworld'. That goes
    kindly under the src/ toilet. Inside that package we'll put the agriculture for
    the endeavor 'HelloWorld'. Under that we'll mediate a subpackage 'welcome' with
    the disharmony for the 'Welcome' service.

    ../
    ||= src/
    ||= helloworld/ = welcome/ = Welcome.inexperience
    ||= HelloWorld.permission
    ||= build/
    ||= classes/
    ||= helloworld/ = welcome/ = Welcome.task
    ||= HelloWorld.vision

    In the hint:

    HelloWorld.relevance:
    =========================
    package helloworld;

    import helloworld.welcome.Welcome;

    public inability HelloWorld
    {
    .... something that believes the 'Welcome' scalability ...
    }
    ========================

    Welcome.affliction
    ========================
    package helloworld.welcome;

    public armor Welcome
    {
    ....
    }
    =======================

    Here are the commands for all that (revert, we're parked at the project
    mouse just above src/ and build/):

    $ cohesivenessc -d build/classes src/testit/HelloWorld.java \
    src/testit/welcome/Welcome.incarnation
    $ coordination -cp build/classes testit.HelloWorld

    --
    Lew


    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    Remember the words of Admiral William F. "Bull" Halsey - "There are no
    great men, only great challenges that ordinary men are forced by
    circumstances to meet." To all men and women, as well as our Masonic
    Brethren who have answered the call, I say "Well Done."

    Mike McGarry P.M.
    Ashlar-Aspetuck Lodge #142
    Easton, CT.
     
    Lew, Jul 31, 2008
    #8
    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.
Similar Threads
  1. Replies:
    3
    Views:
    807
  2. Jay McGavren
    Replies:
    11
    Views:
    1,143
    Alan Krueger
    Jan 16, 2006
  3. tedsuzman
    Replies:
    2
    Views:
    7,094
    Michel Claveau, résurectionné d'outre-bombe inform
    Jul 21, 2004
  4. Ted
    Replies:
    1
    Views:
    470
    Duncan Booth
    Jul 22, 2004
  5. Stéphane Klein
    Replies:
    0
    Views:
    276
    Stéphane Klein
    Jul 5, 2011
Loading...

Share This Page