Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > import statement

Reply
Thread Tools

import statement

 
 
varun chadha
Guest
Posts: n/a
 
      07-29-2008
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;
 
Reply With Quote
 
 
 
 
Donkey Hot
Guest
Posts: n/a
 
      07-29-2008
varun chadha <> wrote in news:5c94a490-51cd-4cf2-
b5a3-:

> 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.
 
Reply With Quote
 
 
 
 
Lew
Guest
Posts: n/a
 
      07-29-2008
On Jul 29, 3:29*pm, varun chadha <varun.con...@gmail.com> 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
 
Reply With Quote
 
Mark Space
Guest
Posts: n/a
 
      07-29-2008
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.




 
Reply With Quote
 
varun chadha
Guest
Posts: n/a
 
      07-30-2008
> 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
{...}

 
Reply With Quote
 
Donkey Hot
Guest
Posts: n/a
 
      07-30-2008
varun chadha <> wrote in news:95150ab5-4d56-42e7-
b992-:

>> 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
{...}
 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-30-2008
On Jul 29, 3:29=A0pm, varun chadha <varun.con...@gmail.com> 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.

 
Reply With Quote
 
Lew
Guest
Posts: n/a
 
      07-31-2008
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.

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
imp.find_module don't found my module but standard import statement can import this module… why ? Stéphane Klein Python 0 07-05-2011 07:36 AM
if statement that, when false, skips first statement in its block, executes second? Jay McGavren Java 11 01-16-2006 05:49 PM
How do I do a conditional statement in a constant statement? tkvhdl@gmail.com VHDL 3 12-16-2005 06:13 PM
exec "statement" VS. exec "statement in globals(), locals() Ted Python 1 07-22-2004 08:51 AM
exec "statement" VS. exec "statement" in globals(), locals() tedsuzman Python 2 07-21-2004 08:41 PM



Advertisments
 



1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57