1

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();
    }

}
4
  • you should inform yourself what static means, i guess you have a basic missunderstanding here. Commented Nov 20, 2015 at 7:44
  • I have edited the tag, I know, it has nothing to do with my question here Commented Nov 20, 2015 at 7:56
  • When you call DataCreator.serialize it calls the serialize method in DataCreator nothing magical happens to call main() Commented Nov 23, 2015 at 19:23
  • The real question is what is the point of the RunPattern class, you could delete it and just use DataCreator instead. Commented Nov 23, 2015 at 19:24

2 Answers 2

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

So, what is the use of the second main() method ? What is the use of a program having another main method in another class? How do I call another main method?
You can simply call the main method of the other class, if you feel to do so. I do not know its purpose, but reading its code ... you can pass a file name as an argument to it, whereas the method RunPattern.main looks more like a test class with a hardcoded file name.
@edgards normally the "main" main class is written inside the manifest file, which will be the entry point for your .jar file. But you could also acces another main method by explicitly invoking a main in a specific class by using java -cp package.class. A usecase might be that you are making a selfcalling jar to prevent the jar blocking another application.
0

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.

2 Comments

So, what is the use of the second main() method ? What is the use of a program having another main method in another class? How do I call another main method?
The comments below Seelenvirtuose`s post answered that more or less already. In general there is not a real use to have more than one Main Method. You call another main method as you would call any other static function. DataCreator.main(new String[]{"testfile.txt"});

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.