68

How can I include one java file into another java file?

For example: If I have 2 java file one is called Person.java and one is called Student.java. How can I include Person.java into Student.java so that I can extend the class from Person.java in Student.java

flurbius
  • 957
  • 7
  • 14
Jin Yong
  • 42,698
  • 72
  • 141
  • 187

4 Answers4

76

Just put the two files in the same directory. Here's an example:

Person.java

public class Person {
  public String name;

  public Person(String name) {
    this.name = name;
  }

  public String toString() {
    return name;
  }
}

Student.java

public class Student extends Person {
  public String somethingnew;

  public Student(String name) {
    super(name);
    somethingnew = "surprise!";
  }

  public String toString() {
    return super.toString() + "\t" + somethingnew;
  }

  public static void main(String[] args) {
    Person you = new Person("foo");
    Student me = new Student("boo");

    System.out.println("Your name is " + you);
    System.out.println("My name is " + me);
  }
}

Running Student (since it has the main function) yields us the desired outcome:

Your name is foo
My name is boo  surprise!
Chris Bunch
  • 87,773
  • 37
  • 126
  • 127
  • 10
    Don't you need some sort of import statement at the top of Student.java? What does that import statement look like? – Andrew Farrell Sep 01 '15 at 01:27
  • 9
    @AndrewM.Farrell No, an import statement is not necessary. Simply place the files in the same directory and compile them. They will be able to "see" each other just fine. – ApproachingDarknessFish Sep 25 '15 at 22:26
  • Your code must be properly compiled too. For example, on a command line do ```javac Person.java``` then ```javac Student.java``` and finally ```java Student.java``` – tommmm Nov 28 '22 at 08:47
33

What's missing from all the explanations is the fact that Java has a strict rule of class name = file name. Meaning if you have a class "Person", is must be in a file named "Person.java". Therefore, if one class tries to access "Person" the filename is not necessary, because it has got to be "Person.java".

Coming for C/C++, I have exact same issue. The answer is to create a new class (in a new file matching class name) and create a public string. This will be your "header" file. Then use that in your main file by using "extends" keyword.

Here is your answer:

  1. Create a file called Include.java. In this file, add this:

    public class Include {
        public static String MyLongString= "abcdef";
    }
    
  2. Create another file, say, User.java. In this file, put:

    import java.io.*;
    
    public class User extends Include {
        System.out.println(Include.MyLongString);
    }
    
Shadow The GPT Wizard
  • 66,030
  • 26
  • 140
  • 208
FractalSpace
  • 5,577
  • 3
  • 42
  • 47
22

Java doesn't use includes the way C does. Instead java uses a concept called the classpath, a list of resources containing java classes. The JVM can access any class on the classpath by name so if you can extend classes and refer to types simply by declaring them. The closes thing to an include statement java has is 'import'. Since classes are broken up into namespaces like foo.bar.Baz, if you're in the qux package and you want to use the Baz class without having to use its full name of foo.bar.Baz, then you need to use an import statement at the beginning of your java file like so: import foo.bar.Baz

Jherico
  • 28,584
  • 8
  • 61
  • 87
5

You don't.

If you want to extend Person with Student, just do:

public class Student extends Person
{
}

And make sure, when you compile both classes, one can find the other one.

What IDE are you using?

Pablo Santa Cruz
  • 176,835
  • 32
  • 241
  • 292
  • Before I do extends for my public class, do not me have to including the file for Person into Studnet 1st? Do you know the statement for include file in java? For example in php the statment is include('directoty/file_name'); – Jin Yong May 19 '09 at 02:05
  • Hi Jin. No, you don't need to "include" the file for Person. You just extend the class. If classes are not in the same package, you "import" the class. But there is no need to include the file. In fact, you can't do that. Even if you want to. Are you using some kind of IDE? Eclipse? Netbeans? – Pablo Santa Cruz May 19 '09 at 02:16
  • It's automatic if the classes are in the same package (including default packages for source files that don't have an explicit package). – Tom Hawtin - tackline May 19 '09 at 02:18
  • No. Java will look for the class in your [Classpath][1]. If it's in the same directory, and you're not using packages, then it'll find it. [1]: http://en.wikipedia.org/wiki/Classpath_%28Java%29 – Matt G May 19 '09 at 02:19