-2

I know this question has been asked many times but I still don't understand how the import statement works. I have a example of my problem as follows.

I have my main file for a simple game in the path: C:\myjavafiles\game.java, I have a file for creating Dwarves in a path called: C:\myjavafiles\dwarf.java, I also have a file for declaring job types in: C:\myjavafiles\support\jobTypes.java. My question is, how could I import dwarf.java and jobTypes.java into game.java if I have the code in each file as follows:

game.java:

// import dwarf.java and jobTypes.java here
class game {
 public static void main(String args[]) {
  // Do something
 }
}

dwarf.java:

public class dwarf {
 public dwarf() {
  // setup dwarf
 }
}

jobTypes.java:

public class jobTypes {
 public jobTypes() {
  // Do something
 }
}

Thank you for you time.

EDIT:

I've add dwarf.java to the package support. And I've add this statement to game.java:

import support.*;

But that doesn't work.

  • 4
    https://docs.oracle.com/javase/tutorial/java/package/packages.html – Luka Jacobowitz May 02 '16 at 13:54
  • 1
    Have a look into package declaration. Also note that class names should begin with a capital letter by convention. – martijnn2008 May 02 '16 at 13:54
  • @LukaJacobowitz that didn't help any, I don't want to know about packages. – Programmer2120 May 02 '16 at 13:57
  • Well if you don't want to know about them, forget about importing – Luka Jacobowitz May 02 '16 at 13:58
  • @Programmer2120 - So why do you think that yet another explanation is going to help you understand where the many previous explanations have not helped? – Stephen C May 02 '16 at 13:59
  • 3
    @StephenC Because as of yet I haven't seen anyone show a example, they just go into long winded speeches about the import statement. (http://stackoverflow.com/questions/12620369/how-java-import-works) – Programmer2120 May 02 '16 at 14:02
  • @LukaJacobowitz Why? – Programmer2120 May 02 '16 at 14:02
  • 1
    The import statement ebabies you to use classes stored in other packages. So you must understand what a package is before you can understand the import statement – Tobias Brösamle May 02 '16 at 14:06
  • If you don't understand packages, you won't understand how to import classes. What might be confusing you is that your directory structure (where you're storing your *.java files) must reflect the package structure. If every class is in the same package, you do not need to use import statements for those classes, but you still will need them for other classes, e.g., `java.io.InputStream`. – Mike Harris May 02 '16 at 14:59
  • Where is a good place to read up of packages? – Programmer2120 May 02 '16 at 15:36

1 Answers1

0

import Statements An import statement is a way of making more of the functionality of Java available to your program. Java can do a lot of things, and not every program needs to do everything. So, to cut things down to size, so to speak, Java has its classes divided into "packages." Your own classes are part of packages, too.

No import Needed

The simple Hello.java program doesn't have any import statements:

public class Hello{
  public static void main(String arg[]){
  System.out.println("Hello.");
}

}

Everything in the program is already available to the compiler. The compiler can access any class in the java.lang package without needing an import statement. It can also access any classes in the "local" package, which is any classes defined in files in the same directory as the program being compiled that aren't part of another package (that is, they don't have a package statement at the start of the file.)

import Required

Anything that isn't in the java.lang package or the local package needs to be imported. An example is the Scanner class. If you look up the Scanner class in the Java API Specification, you'll see that it is in the java.util package.

Eritrean
  • 15,851
  • 3
  • 22
  • 28