Why main method is written only in object not class?

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



Why main method is written only in object not class?



How does a main method gets called in scala ? Why does a main method gets called in when it is written only in object but not in class ?





Your assumption is wrong. Main methods can be written outside an object, in a class.
– Maroun
May 28 at 15:15





@Maroun since when that?
– Andrey Tyukin
May 28 at 15:16





@AndreyTyukin It's not possible to define your own method, called main inside a class? I'm not Scala expert, but familiar a bit with the language.
– Maroun
May 28 at 15:18


main





@Maroun It's certainly possible, but I see no reason why it should be treated as an entry point of the application. It will be just an ordinary method that accidentally carries the name "main", but that's not what is usually understood under "main method" on the JVM. The spec talks about objects, and about mixing in the main method into objects. Not about classes.
– Andrey Tyukin
May 28 at 15:20


main





I'm not sure what the question is actually asking. What would be a possible answer? "Because the spec says so."? OK, that's the convention, now what's the question?
– Andrey Tyukin
May 28 at 15:22




3 Answers
3



Because the specification says so:



A program is a top-level object that has a member method main of type (Array[String])Unit. Programs can be executed from a command shell. The program's command arguments are passed to the main method as a parameter of type Array[String].


(Array[String])Unit


Array[String]



The main method of a program can be directly defined in the object, or it can be inherited.



It speaks only about top-level objects, not classes. If you define a main method in a class, then it will be just an ordinary method that you can invoke on the instances of this class. Unless you define a top-level object that inherits the main from this class / trait, this method called main will not be treated as an entry point of the application.


main


main


main



The main method must be a static method. In Scala to create a static method you put it in an object. Methods in a class are not static.



In the scala language they decided to separate class, which hold only instance behavior and state, and object which hold static behavior and state. This is different from java where classes hold both instance and static members where something is made static using the static keyword.


static





1 more dout in java main method is static so jvm does not needs to create its object for calling . Does the same thing happens in scala when we write method in object ?
– USER
May 29 at 5:59





Are you asking about the Scala Language Specification or private internal implementation details of the JVM implementation of Scala or some other implementation of Scala (like Scala-native, Scala.js, or Scala.NET)?
– Jörg W Mittag
May 29 at 16:54



It is because in scala the only way to define a method static is object class. And also it is necessary only one instance of main class is created , not multiple instances. That's why it is object class






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard