Search code examples
javalinuxjavac

How to import a class to a java program using Javac?


Introduction

I'm using the terminal and Javac to compile and run my java programs. Individual programs work fine. However, when i want to import a class into my main Java program,I receive errors.

Source Code - my class (Name.java)

public class Name {

//Fields
private String firstName;
private String familyName;


//Constructors
public Name() {
    firstName = "";
    familyName = "";
}

public Name(String firstName, String familyName) {
    this.firstName = firstName;
    this.familyName = familyName;
}


//Methods
public void setFirstName(String firstName) {
    this.firstName = firstName;
}

public void setFamilyName(String familyName) {
    this.familyName = familyName;
}

public String getFirstName() {
    return firstName;
}

public String getFamilyName() {
    return familyName;
}

public String getFullName() {
    if (firstName.equals("") && familyName.equals("")) {
        return "";
    } else {
        return firstName + " " + familyName;
    }
}

@Override
public String toString() {
    return "Name:[firstName=" + firstName + ", familyName=" + familyName + "]";
}
}

Main - NameDemo.java

import Name;

public class NameDemo {

public static void main(String[] args) {
    /* 
     * See below evidence of how arrays can store elements of a custom reference
     * type that you create (Name in this case), as well as both existing
     * reference types (e.g. String) or primitive types (e.g. int), that you have
     * previously worked with.
     */

    //declare array to hold four Name objects
    Name[] friends = new Name[4];

    //create Name object and assign reference into array
    Name joe = new Name("Joe", "Bloggs");
    friends[0] = joe;

    //create Name objects and assign reference directly into array
    friends[1] = new Name("Fred", "Perry");
    friends[2] = new Name("Diti", "Hammond");
    friends[3] = new Name("Bulsar", "Raymond");

    //loop through each element
    for(int i = 0; i < friends.length; i++) {
        //use accessor method to output full name of each
        System.out.println(friends[i].getFullName());
    }

    //declare boolean used as a flag
    boolean exists = false;

    //loop through each Name object using a for-each loop (you will see more on this when we cover ArrayLists)
    for (Name nm : friends) {
        //check family name
        if (nm.getFamilyName().equals("Smith")) {
            exists = true;
        }
    }

    //output result of search
    if (exists) {
        System.out.println("There is a person with family name of Smith");
    } else {
        System.out.println("There is not a person with family name of Smith");
    }

    //Checking output of toString
    System.out.println("\n" + joe.toString());

}

}

The problem

Both files are inside the same directory. AS well as this, i've imported the class into the main program source code. I then proceed to try and compile both.

import Name;

javac Name.java
javac NameDemo.java
NameDemo.java:4: error: '.' expected
import Name;
           ^
NameDemo.java:4: error: ';' expected
import Name;
            ^
2 errors

Why is this occurring, am i supposed to compile the class? or does the class need to end in .class or .java? as well as this is this a syntax issue with the import?


Solution

  • So:

    1. You should download IDE, I would recommend you Eclipse. This will help you to program later.
    2. You should pass names of EVERY file to javac. This way javac will know that method from class #2 exists and there is no need of bailing out.
    3. There is NO need of importing - files are in same package - that means, in same directory. Javac wanted you to pass something in this format: import packagename.classname; while you provided only classname.

    Class as compiled bytecode needs to have extension .class, while class in source code like you've written, needs to have extension .java.