The Artima Developer Community
Sponsored Link

Java Answers Forum
Importing classes again

4 replies on 1 page. Most recent reply: Dec 8, 2005 3:02 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 4 replies on 1 page
Joe Spoot

Posts: 4
Nickname: sllobb
Registered: Nov, 2005

Importing classes again Posted: Dec 1, 2005 11:28 AM
Reply to this message Reply
Advertisement
Thanks for the response to my earlier post. But that wasn't what I was looking for. Sorry for being too vague.

Ok, here's the deal. I have two java files saved on my computer. One is called Family. The other is called Person. Family is the one I am executing. However, in Family, I want to be able to create and use objects of type Person. I know there is some line you have to type in order to tell the program, Family, that in order to understand the line:

PERSON person = new PERSON();

it must look to my other file called Person. What is that line(s) of code?


Jody Brown

Posts: 8
Nickname: jody
Registered: Mar, 2002

Re: Importing classes again Posted: Dec 1, 2005 7:50 PM
Reply to this message Reply
If the two classes are stored in the same package (on the file system this will be in the same folder) you do not need to add anything to use the Person class. If however they are located in different packages (or folder) you need to use the import statement, placed at the top of the source file you wish to call it in. An example of this is as follows:

Class "Family" is defined in the folder C:/afolder/anotherfolder/Family.java and class "Person" is held in C:/afolder/anotherfolder/yetanotherfolder/Person.java, then in your Family class you will need the following line at the top of the class:

import afolder.anotherfolder.yetanotherfolder.Person;

I hope this was clear enough.

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Importing classes again Posted: Dec 1, 2005 10:53 PM
Reply to this message Reply
You must understand that Java uses files, but is not file-based.

With another class loader you would also be able to put all classes in one file.

If the JVM should find a class you specify, it must be in the classpath. The package name must be the same as the subdirectory the file is in, just exchange "\" with "."
Example.
classpath = c:\MyProgram\
Class1 lies in c:\MyProgram\package1\subPackage\Class1.class
the package of Class1 MUST be "package1.subPackage"

If you compile the program, you must do it using "c:\MyProgram" as classpath.

You can of course add more directories or JAR files to the classpath.

for example:
classpath = c:\MyPersonProgram;c:\MyFamilyProgram;c:\somwhereElse\MyFinishedProgram.jar
It does not matter in which directory / JAR file your classes are stored (only if you have classes with the same classpath and name, the first one will be used).
You can access all files in the path using package and class name.



One note to naming.
1. Class names should begin with a capital letter, but should also contain small letters ("MyClass");
2. Packages and variables should have a lower case character at the beginning ("myPackage")

Only constants should be written in capital letters ("direction.LEFT", where direction can be a instance variable or a enumeration. If "direction" would be a class, it should be "Direction.LEFT").
Constants usually are defined by setting a class variable to final or using a enumeration.

Joe Spoot

Posts: 4
Nickname: sllobb
Registered: Nov, 2005

Re: Importing classes again Posted: Dec 5, 2005 1:04 PM
Reply to this message Reply
Thanks for the help. I've done everything you've said (at least I think so), but when I try to compile, I get the following error message:

family.java:13: '.' expected
import person;
^

Both family and person are in the same folder. My import statement looks like this:

import person;

What am I doing wrong?

Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Importing classes again Posted: Dec 8, 2005 3:02 AM
Reply to this message Reply
If they are in the same folder, you don't need to import.

Note: ClassNames should begin with a capital letter, so should the filename.

You should have something like this.

c:\TestFolder\Person.class
c:\TestFolder\Family.class


Family.java looks like this:
 class Family {
}


Now here's an example for different Packages.

c:\TestFolder\subPack\Person.class
c:\TestFolder\Family.class

class Person looks like this
package subPack;
public class Person {
}

class Family looks like this:
import subPack.Person; //or import subPack.*;
class Family {
    Person myPerson; //see? You just don't need to write subPack.Person, that's all the import statement does.
}


Last example.
Let's put Family in another package.
c:\TestFolder\subPack\Person.class
c:\TestFolder\anotherPackage\Family .class

package subPack;
public class Person {
}

class Family looks like this:
package anotherPackage;
import subPack.Person; //or import subPack.*;
class Family {
    Person myPerson; //see? You just don't need to write subPack.Person, that's all the import statement does.
}



The error you got was a pure syntax error. You probably forgot to put a ";" after the package definition.

Flat View: This topic has 4 replies on 1 page
Topic: objects placed into arrays Previous Topic   Next Topic Topic: Help with Cut Text Method

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use