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.

Edited by pinsickle: weird wording

8 Years
Discussion Span
Last Post by dimapoter
//In a file called First.java
public class First{
//File called Second.java
public class Second{
First f = new First();


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

Votes + Comments
very helpful

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.

This question has already been answered. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.