-3

The file compiles and works as expected but when I try to use it in another file it keeps saying when I attempt to compile. So clearly I don't know how to import code from another file correctly. How do I import the file for use within , I have read many explanations but they all require an IDE which I don't plan on using, I am using notepad++.

3
  • its the import statement on Centralize class, its looking for package. Commented Jun 3, 2014 at 14:32
  • Do you compile both files together or separately? Also, I don't see any package declaration in your ReadFile.java while you seem to expect one in Centralise (import ReadFile.*;). Commented Jun 3, 2014 at 14:34
  • What should I write on the import to be able to use ReadFile then? Commented Jun 3, 2014 at 14:38

1 Answer 1

3

You do import ReadFile.*; but ReadFile is not the package name but the class name. Package allows you to categorize your classes. Take a look at http://en.wikipedia.org/wiki/Java_package for more details.

Put your ReadFile.java into a package (for example: package org.your-company.io) then in the second class:

import org.your-company.io.ReadFile;

The instruction import somepackage.* indicates that you can use any classes from the package somepackage in your current class. For example, if I do:

import java.sql.*;

I will be able to call, in my code, directly:

Date dsql = ... // java.sql.Date
DriverManager driver = ... // java.sql.DriverManager
etc.

Edit

As Dukeling mentionned in a comment, if your classes are in the same "folder" (I mean package), you can remove your instruction import ReadFile.* which is wrong and useless.

Sign up to request clarification or add additional context in comments.

13 Comments

Good answer, but you should elaborate. What should he do? Why is that not working?
... or if it's in the same package (which could be no package - which appears to currently be the case), there's no need to import it.
What is a package? I just have a folder with three files: ReadFile.java, ReadFile.class and Centralise.java. How would I use ReadFile within Centralise?
So a package is just a folder. If your files are in the same folder, you don't need any imports.
@user3701257 : You can take a look on Oracle tutorial which will explain you the package concept: docs.oracle.com/javase/tutorial/java/package
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.