I have two classes where each class has one main method, the main class where it will become the entry point is the RunPattern class. The question is, which method in DataCreator class will be called first when DataCreator.serialize(fileName) is called in the RunPattern class ? is it the main() method or serialize()? Is it different than static scope?
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.io.ObjectOutputStream;
import java.io.FileOutputStream;
public class RunPattern {
public static void main(String args[]){``// The first main() method
if(!new File("data.ser").exists()){
DataCreator.serialize("data.ser");
}
}
}
class DataCreator {
private static final String DEFAULT_FILE = "data.ser";
public static void main(String args[]){ // the second main
String fileName;
if(args.length == 1){
fileName = args[0];
}else{
fileName = DEFAULT_FILE;
}
serialize(fileName);
}
public static void serialize(String fileName){
try{
serializeToFile(createData(), fileName);
}catch(IOException exc){
exc.printStackTrace();
}
}
private static Serializable createData(){
ToDoListCollection data = new ToDoListCollectionImpl();
ToDoList listOne = new ToDoListImpl();
ToDoList listTwo = new ToDoListImpl();
ToDoList listThree = new ToDoListImpl();
listOne.setListName("Daily Routine");
listTwo.setListName("Programmer Hair Washing Product");
listThree.setListName("Reading List");
listOne.add("get up");
listOne.add("Brew cuppa java");
listOne.add("Read JVM Times");
listTwo.add("Latter");
listTwo.add("Rinse");
listTwo.add("Repeat");
listTwo.add("eventually throw too much conditioner");
listThree.add("the complete of duke");
listThree.add("how green was java");
listThree.add("URL, sweet URL" );
data.add(listOne);
data.add(listTwo);
data.add(listThree);
return data;
}
public static void serializeToFile(Serializable data, String fileName) throws IOException {
ObjectOutputStream serOut = new ObjectOutputStream(new FileOutputStream(fileName));
serOut.writeObject(data);
serOut.close();
}
}
DataCreator.serializeit calls theserializemethod inDataCreatornothing magical happens to callmain()RunPatternclass, you could delete it and just use DataCreator instead.