import statement

V

varun chadha

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;
 
D

Donkey Hot

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.
 
L

Lew

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.
 
M

Mark Space

varun said:
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.
 
V

varun chadha

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
{...}
 
D

Donkey Hot

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
{...}
 
L

Lew

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.
 
L

Lew

Farter said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,528
Members
45,000
Latest member
MurrayKeync

Latest Threads

Top