-1

I know C++ at a decent level and I am trying to learn java. This will be a silly question but I cannot figure out how to import a .java file into another. I am at Eclipse IDE and in my project I have two files:

FileReader.java

Entry.java

I want to import the Entry.java in the other file but no matter what I do I get an error. Can you help me? Thx in advance.

FileReader.java :

import java.io.*;

     class FileReader {
            public static void main(String[] args) throws Exception {

                System.out.println("Hello, World");
                Entry a(10,"a title","a description");
                a.print();

            }
        }

Entry.java:

public class Entry{
    int ID;
    String title;
    String description;


    public Entry(int id, String t,String d){
        ID=id;
        title=t;
        description=d;

    }

    public void print(){
        System.out.println("ID:"+ID);
        System.out.println("Title:"+title);
        System.out.println("Description:"+description);
    }
}

At this state I get an error that Entry cannot be resolved as a variable. So I believe that it is related to the import.

JmRag
  • 1,443
  • 7
  • 19
  • 56
  • Are they in the same package? – Sionnach733 Nov 22 '13 at 00:21
  • `Entry a(10,"a title","a description");` is not valid Java. If they are in the same package (i.e. same directory), the class should be automatically found. – Amadan Nov 22 '13 at 00:22
  • Copy the entry.java to the src folder of the project that is located at workspace and then refresh eclipse, when it show up just click in the error mark near the code line and select "import" – João Silva Nov 22 '13 at 00:22

4 Answers4

3

You need new Entry

The new keyword creates the new object

Entry a = new Entry(10,"a title","a description") 
a.print();

An Entry object is created with the a reference with the above instantiation.

For the import part of your question, if two files are in the same package, no import is needed. If you Entry class was in a different package than your FileReader class, then you would need to import mypackage.Entry

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
3

Firstly

Entry a(10,"a title","a description");

should be

Entry a = new Entry (10,"a title","a description");

If Entry is in the same package then you will not need to import it.

If Entry is in a different package, say com.example then you will need to do

Either

import com.example.Entry;

or

import com.example.*;

The second import will import all classes in the com.example package - usually not such a good thing.

Scary Wombat
  • 44,617
  • 6
  • 35
  • 64
  • Thx I was thinking that it would work as in C++ and the problem would be elsewhere. The package is meant to be the project here or I get something wrong? – JmRag Nov 22 '13 at 00:38
  • @Mario: Eclipse can be quite helpful. If you miss your imports, just hover over the error and it should offer to add the most likely package to your imports, gets it right most of the time. It's also handy when implementing an interface: just add the interface name to the ```implements``` list, hover the error and select to define missing methods rather than having to type all method definitions manually. – DarthJDG Nov 22 '13 at 10:27
2

Try

Entry a = new Entry(/*args*/);

And if you need to import the class, then use the absolute name (package+class) and put it after import above the class declaration

import com.example.you.Entry;
Rogue
  • 11,105
  • 5
  • 45
  • 71
1

In Eclipse you can do Ctrl+Shift+O to resolve all imports.

wvdz
  • 16,251
  • 4
  • 53
  • 90