Results 1 to 3 of 3
  1. #1
    xpoferens1 is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Question Importing a Class

    I've been reading the tutorials and doing some googling but I just cant find the answer to my question:

    Im writing a simple program to understand classes and objects. Basically what I have is a file called Program.java where I have my main method.

    I have another file called Person.java which I want to use to create Person objects. That person can have a name, email adress, phone number, etc.

    I put both these files in the same folder.

    in Program.java my first statement is:

    Java Code:
    import Person.java
    My problem is that when I compile Program.java i get an error message saying that the package Person.java does not exist.

    So my question is, when you create a class that you want to use for objects, how do you import that class into your class with the
    main method so that you can use instances of your other class?

    Thanks in advance,

    -XpoFerenS

  2. #2
    xpoferens1 is offline Member
    Join Date
    Feb 2014
    Posts
    9
    Rep Power
    0

    Default Re: Importing a Class

    I think I just found the answer to my own question.
    You dont actually have to import your class if both classes are in the same folder.
    Is that correct?

  3. #3
    gimbal2 is online now Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    3,731
    Rep Power
    5

    Default Re: Importing a Class

    If they are in the same package, yes. Don't think from the perspective of the file system, you have to think from the perspective of the Java classpath. Classes can exist in folders on disc, they can be in jar files, they can be somewhere else too because Java allows you to provide your own way to load classes.

    You also don't have to import them if they are in the java.lang package. And there you have to understand one thing: there is no such thing as a subpackage. So even though classes in java.lang are automatically on the classpath, for example the classes in java.lang.reflect are NOT. Its a 100% completely different package, even though it starts with 'java.lang' too.
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

Similar Threads

  1. Importing a .java class
    By natdizzle in forum Eclipse
    Replies: 3
    Last Post: 01-15-2012, 12:05 AM
  2. Importing a class folder
    By malaguena in forum Advanced Java
    Replies: 6
    Last Post: 03-15-2011, 12:04 PM
  3. importing class files
    By bizso in forum New To Java
    Replies: 6
    Last Post: 03-28-2009, 02:11 PM
  4. Importing custom class
    By BigRed in forum Eclipse
    Replies: 2
    Last Post: 02-14-2009, 02:12 PM
  5. Importing a Custom Class
    By jfredrickson in forum New To Java
    Replies: 3
    Last Post: 07-11-2007, 11:23 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •