This week's book giveaway is in the Servlets forum.
We're giving away four copies of Murach's Java Servlets and JSP and have Joel Murach on-line!
See this thread for details.
The moose likes Beginning Java and the fly likes import statement Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "import statement" Watch "import statement" New topic
Author

import statement

Jason sigel
Ranch Hand

Joined: Oct 07, 2003
Posts: 30
I am new to java programming . Is this statement
import com.sybase.jdbc.*;
means I should be connected to net while running this programs.
I want to know what is the difference between package and import
statement.what I know is that if I give package directory in classpath then
all files would be included automatically else i have to use import
statement.
I am using classpath as .
and when I run this example
file://: c07:Music.java
// Inheritance & upcasting
package c07;
class Note {
private int value;
private Note(int val) { value = val; }
public static final Note
middleC = new Note(0),
cSharp = new Note(1),
cFlat = new Note(2);
} // Etc.
class Instrument {
public void play(Note n) {
System.out.println("Instrument.play()");
}
}
// Wind objects are instruments
// because they have the same interface:
class Wind extends Instrument {
// Redefine interface method:
public void play(Note n) {
System.out.println("Wind.play()");
}
}
public class Music {
public static void tune(Instrument i) {
// ...
i.play(Note.middleC);
}
public static void main(String[] args) {
Wind flute = new Wind();
tune(flute); // Upcasting
}
} ///:~

it give me error and when I remove package statement then I could run the
program.

jaideep
Steven YaegerII
Ranch Hand

Joined: May 31, 2000
Posts: 182
I'll take a swing. First, when using both package and import - first comes the package statement, second comes the import statement, and then comes the class declaration.
Use a package statement when you have several files that are all related. You probally have other files with 'package c07;' at the top. Also keep in mind the default or package access when not declaring a public, private, or protected keyword. All the classes in your program that have none of the above access identifiers can be accessible from every file in the package.
Use the import statement to make other classes/API available for use: such as import javax.swing.*; ...for making graphical user interfaces (GUI's). Some of this may collide with more expert answers. Hope this helps a little.
[This message has been edited by Steven YaegerII (edited August 21, 2000).]

[This message has been edited by Steven YaegerII (edited August 21, 2000).]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: import statement
 
Similar Threads
Upcasting.....Its confusing
Problem geeting value in JSP after type casting Gurus Please help me
non-static variable super cannot be referenced from a static context
class methods vs instance method
OOP