How to include existing code/class file when compiling in Java

23,910

Solution 1

You can use import the A.java class in B.java like this

import A.java;

By doing like this A class will be available in B class.

Java Packages Tutorial

Solution 2

You're looking for the import statement, and the Using Package Members tutorial.

Solution 3

Use can import classes and packages.

    import java.io.*; //packages
    import codes.dir.jar; //classes in your packages

I'd advise you to use an IDE for coding java, it would make you debug faster. I recommend Netbeans or Eclipse || http://www.netbeans.org/ or http://www.eclipse.org/

Share:
23,910
Jinal Kothari
Author by

Jinal Kothari

Updated on July 18, 2020

Comments

  • Jinal Kothari
    Jinal Kothari almost 4 years

    I am a complete newbie with Java. I use notepad++ to write Java code, and then compile and execute using command line on the Windows Platform

    How do I reuse existing code or a class file? For instance, I have written a class in file A.java (with the corresponding compiled class file available) and wish to use that class in B.java. Do I need to specify it in the B.java file (sort of lie c/c++) or using compiler options. What compiler options?

  • Jinal Kothari
    Jinal Kothari almost 13 years
    Thanks for the answer. I am not yet using an IDE ... I am still at the "Hello World" stage.