I'm trying to learn Java, pretty easy so far as my previous experiance is with C/C++. I was just curious how to call java classes from different files. That way I don't have to have main as well as all of my classes crammed into one file. I'm sure the book that I am working out of will eventually tackle this issue. However, it is driving me crazy (can't stand my code to look crammed together and cluttered.) Thanks for the help.

Kind of worded that funny.

For example in C/C++ you could do #include "fileName I want to refer to"
I'm just asking for the java equivalent.

Recommended Answers

All 2 Replies

//In a file called First.java
public class First{
... 
}
//File called Second.java
public class Second{
..
main(){
First f = new First();
}

}

Put the classes in a package together... then you can create a new Object and use the class's methods.

commented: very helpful +1

Either import the package.* or package.classfilename, where package is the package the your class file is directly in. and you will be able to call the class statically (class.*()) or as an Object Class (new class). Note that usually java only lets you make the class name match the file name.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.