Reputation: 930
Is it possible to call a main method in one object from a main method in another? I have the following classes and was wondering how to call two separate main methods within one program run:
object MongoUpload {
def main(args: Array[String]): Unit = {
.. upload to Mongo ..
// Want to upload to Oracle here
}
}
object OracleUpload {
def main(args: Array[String]): Unit = {
.. upload to Oracle
}
}
Does anything make main
unique among methods? Can I just call one from another?
Upvotes: 3
Views: 2296
Reputation: 930
object foo {
def main(args: Array[String]): Unit = {
println("qux")
}
}
object bar {
def main(args: Array[String]): Unit = {
println("baz")
foo.main(null)
}
}
Running main
in bar
gives the following output:
baz
qux
The same can also be replicated to main methods with arguments, as in the following example:
object foo {
def main(args: Array[String]): Unit = {
println(args(0) + " " + args(1))
}
}
object bar {
def main(args: Array[String]): Unit = {
... some processing ...
foo.main(Array["Hello", "World"])
}
}
Running main
in bar
gives the following output:
Hello World
Whether or not it leads to clear and readable code is another question :)
Upvotes: 7