Reputation: 99
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();
}
}
Upvotes: 1
Views: 249
Reputation: 307
The main method of DataCreator will never be called this way.
Some info about setting the main method in the Manifest-File
Since you are usually developing within an IDE, this is done for you automaticially.
But you can choose which mainmethod is your entrypoint in the Runconfiguration.
Upvotes: 0
Reputation: 20608
Well. If you run your program with
java -cp ... RunPattern
then the main
method of your RunPattern
class is invoked. This in turn calls
DataCreator.serialize("data.ser");
which simply invokes the method serialize
of your DataCreator
class. The main
method in this class is not involved.
Upvotes: 1