Been going through Core Java Fundamentals (Horstmann/Cornell) 9th edition, but sometime after chapter 5 or so both the exercise files and sample code stop having a "main" method but the book acts as if they should compile and run, etc...
Very confused since java apps need a main to run. Anyone familiar with these texts that can shed some light for me. Feeling pretty dumb right now :)
Many libraries are developed without the purpose of running as a standalone application. Joda-Time is one such example. It is a library you can use to simplify the manipulation of date and time data. Admittedly, the source repository I linked does have main methods in it, but that's because they are used in its unit tests.
The point of chapter five in Core Java Fundamentals is to introduce class inheritance, such that Vehicle can be superclass of Boat can be superclass of Sailboat can be superclass of Sunfish. The book probably has an example like Person > Employee > Manager, and eventually in that same chapter you may end up seeing a test class like TestManager that actually does have a main method using Manager.
Suppose I write a similar book and I show this over simplified version of the Person example. I create a project in Eclipse and I call it Personnel. I add a package org.company
and inside that package I add these 3 classes:
Person
package org.company;
public class Person {
public String name;
public Person() {
}
}
Employee
package org.company;
public class Employee extends Person {
public String employeeID;
public Employee() {
}
}
Manager
package org.company;
import java.util.ArrayList;
import java.util.List;
public class Manager extends Employee {
public List<Employee> subordinates ;
public Manager() {
subordinates = new ArrayList<Employee>();
}
}
I may now assume you can create your own project and incorporate these classes I've written. The fact I named mine Personnel is irrelevant. Call yours MyTest1, then don't even bother creating a package, and add one class called TestManager. As long as you A) put my classes two folders down in your project org / company / Person, Employee, Manager, or B) delete the package org.company
from each of them and include them next to your new TestManager class, then you should be able to instantiate them in your test as:
TestManager
import org.company.Employee;
import org.company.Manager;
public class TestManager {
public static void main(String[] args) {
Employee bill = new Employee();
bill.name = "Bill";
bill.employeeID = "1";
Employee george = new Employee();
george.name = "George";
george.employeeID = "3";
Manager jill = new Manager();
jill.name = "Jill";
jill.employeeID = "1234";
jill.subordinates.add(bill);
jill.subordinates.add(george);
//foreach
for (Employee emp : jill.subordinates) {
System.out.println(emp.name);
}
}
}
The files in Eclipse now look like this.
A future task for you to consider is to get a library from the internet such as Joda-Time and see about incorporating it into your project by adding it to the build path.