views:

6109

answers:

5

I'm pretty new in Java, so perhaps this question is not make me sound very stupid. 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 exstend the class from Person.java in Student.java

A: 

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
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
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
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
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
+8  A: 

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
+8  A: 

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
A: 

When logically something seems as if it would be dead simple, then it turns out to be completely convoluted, it makes you just want to quit. I'm sorry, but I don't understand any of the preceding comments about this.

I have not used Java much, so please forgive my ignorance. Here is what I am trying to do:

I have a nice clean algorithm in one .java file, and I want to feed it from an initialized array. The array contains over 40,000 elements, so I want it in a separate file all by itself. How do I do this? Is there seriously no quick way to just say {insert contents of filename.txt here}?

A: 

Whats' 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);

}

FractalSpace