Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8939887
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:54:55+00:00 2026-06-15T10:54:55+00:00

General style question. As I become better at writing functional code, more of my

  • 0

General style question.

As I become better at writing functional code, more of my methods are becoming pure functions. I find that lots of my “classes” (in the loose sense of a container of code) are becoming state free. Therefore I make them objects instead of classes as there is no need to instantiate them.

Now in the Java world, having a class full of “static” methods would seem rather odd, and is generally only used for “helper” classes, like you see with Guava and Commons-* and so on.

So my question is, in the Scala world, is having lots of logic inside “objects” and not “classes” quite normal, or is there another preferred idiom.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T10:54:55+00:00Added an answer on June 15, 2026 at 10:54 am

    As you mention in your title, objects are singleton classes, not classes with static methods as you mention in the text of your question.

    And there are a few things that make scala objects better than both static AND singletons in java-world, so it is quite “normal” to use them in scala.

    For one thing, unlike static methods, object methods are polymorphic, so you can easily inject objects as dependencies:

    scala> trait Quack {def quack="quack"}
    defined trait Quack
    
    scala> class Duck extends Quack
    defined class Duck
    
    scala> object Quacker extends Quack {override def quack="QUAACK"}
    defined module Quacker
    
    // MakeItQuack expects something implementing Quack
    scala> def MakeItQuack(q: Quack) = q.quack
    MakeItQuack: (q: Quack)java.lang.String
    
    // ...it can be a class
    scala> MakeItQuack(new Duck)
    res0: java.lang.String = quack
    
    // ...or it can be an object
    scala> MakeItQuack(Quacker)
    res1: java.lang.String = QUAACK
    

    This makes them usable without tight coupling and without promoting global state (which are two of the issues generally attributed to both static methods and singletons).

    Then there’s the fact that they do away with all the boilerplate that makes singletons so ugly and unidiomatic-looking in java. This is an often overlooked point, in my opinion, and part of what makes singletons so frowned upon in java even when they are stateless and not used as global state.

    Also, the boilerplate you have to repeat in all java singletons gives the class two responsibilities: ensuring there’s only one instance of itself and doing whatever it’s supposed to do. The fact that scala has a declarative way of specifying that something is a singleton relieves the class and the programmer from breaking the single responsibility principle. In scala you know an object is a singleton and you can just reason about what it does.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

general question is i like to build logger class that writes to single log
I find myself having a lot of this in different methods in my code:
I have a question on programming style and C# language design in general, I'd
This isn't a style question. Its more about the proper use of the language
This is a general question on programming style. Let's say I have an object
I'm vastly trying to build a solid and responsive coding style. My general question
I have a lot of code in a big project that has two general
In Python, is it considered better style to: explicitly define useful functions in terms
The general question: Is it bad style if you create a form in rails
I'm currently trying to apply a more functional programming style to a project involving

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.