This week's book giveaway is in the Agile and Other Processes forum.
We're giving away four copies of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java and have Dr. Raoul-Gabriel Urma & Richard Warburton on-line!
See this thread for details.
Win a copy of Real-World Software Development: A Project-Driven Guide to Fundamentals in Java this week in the Agile and Other Processes forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Paul Clapham
  • Bear Bibeault
  • Liutauras Vilda
  • Devaka Cooray
Sheriffs:
  • Knute Snortum
  • Junilu Lacar
  • Henry Wong
Saloon Keepers:
  • Ron McLeod
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
  • Tim Holloway
Bartenders:
  • salvin francis
  • Frits Walraven
  • Piet Souris

Why Main is inside singleton class when that's not a replacement for static?

 
Ranch Hand
Posts: 1360
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Scala requires class containing main method to be singleton. I read that it is because scala does not have a concept of static so it is singleton class. Singleton and static are 2 different things. How does making it singleton help the fact that scala does not have concept of static. Why does its hello world need to have main method in singleton class?

Thanks
 
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you run a Scala application, it needs to call the main method on an object of  your main class. How would the runtime know how to create an object from the main class, unless you provide it with a singleton object?
 
Monica Shiralkar
Ranch Hand
Posts: 1360
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So to call the main method , does it creates 1 instance of singleton class internally and then invoke its main method? For the similar thing , in Java it does not create any object but calls the static method main. Scala doesnt have concept of main so may be for this reason it has been designed to do it in different way.
 
Stephan van Hulst
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Monica Shiralkar wrote:Scala doesnt have concept of main so may be for this reason it has been designed to do it in different way


Nonsense. First, Scala does use a main method, same as Java.

Secondly, singletons were introduced in Scala because the designers wanted to allow developers to make some objects globally accessible. Then they half-assed it by also allowing developers to make these objects mutable.
 
Monica Shiralkar
Ranch Hand
Posts: 1360
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Sorry I had mistyped. I meant to type scala does not have a concept of static.
 
Stephan van Hulst
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Yes, Scala's singleton concept is close to Java's static concept. The difference is that Java's static members can't refer to a current object, while in Scala, members of a singleton can refer to the singleton object.
 
Monica Shiralkar
Ranch Hand
Posts: 1360
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But singleton and static are very different concepts. Static means common to all objects and which do not belong to any object. Singleton means when you can create only 1 object for the class.
 
Stephan van Hulst
Saloon Keeper
Posts: 11462
247
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
They're not so different as you think.

Static members in Java can be seen as members of a class instance. For example, a static field named 'title' in a class named 'Book' can be seen as an instance field on a singleton object of type Class<Book>.

That's not REALLY how Java works, but almost, and it's very similar in concept to Scala's singleton.
 
Monica Shiralkar
Ranch Hand
Posts: 1360
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Stephan van Hulst wrote:They're not so different as you think.

Static members in Java can be seen as members of a class instance. For example, a static field named 'title' in a class named 'Book' can be seen as an instance field on a singleton object of type Class<Book>.

That's not REALLY how Java works, but almost, and it's very similar in concept to Scala's singleton.




Thanks. Yes it can be seen in similar way as shown by you . So when we run the scala hello world program, it seems the JVM first  creates instance of the singleton class and then invokes the main method.
 
Stephan van Hulst
Saloon Keeper
Posts: 11462
247
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Correct.
 
What could go wrong in a swell place like "The Evil Eye"? Or with this tiny ad?
Java file APIs (DOC, XLS, PDF, and many more)
https://products.aspose.com/total/java
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!