This week's book giveaway is in the Java in General forum.
We're giving away four copies of Think Java: How to Think Like a Computer Scientist and have Allen B. Downey & Chris Mayfield on-line!
See this thread for details.
Win a copy of Think Java: How to Think Like a Computer Scientist this week in the Java in General forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

import statement

 
Jason sigel
Ranch Hand
Posts: 30
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 182
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic