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

  • Home
  • SEARCH
  • 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 8375689
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T15:14:28+00:00 2026-06-09T15:14:28+00:00

Below class does not compile, if I declare Functions as an object instead of

  • 0

Below class does not compile, if I declare Functions as an object instead of class I can run the method fac using Functions.fac(3) . Does it make sense in scala to attempt to run a class like this ? How can the below code be modified so that it runs without changing to object instead of class ?

class Functions {

  def fac(n : Int) = {
    var r = 1;
    for(i <- 1 to n) r = r * i;
    r
  }
     def main(args:Array[String]) = {
      Functions f = new Functions();

      print(f.fac(3));
  }

}
  • 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-09T15:14:29+00:00Added an answer on June 9, 2026 at 3:14 pm

    The most obvious problem in your code is that you have Functions f = .... This is Java syntax, so in Scala you need it to say val f: Functions = ...

    Second, Scala makes a larger distinction between static and non-static things than Java. In Scala, something that’s static (such as a main method) is declared in an object. So you should separate the object (static) parts from the class parts.

    Third, your fac function could be more simply written as (1 to n).product.

    Finally, you don’t need the semi-colons.

    class Functions {
      def fac(n: Int) =
        (1 to n).product
    }
    
    object Functions {
      def main(args: Array[String]) = {
        val f: Functions = new Functions()
        print(f.fac(3))
      }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My code does not compile. Below is my code template <typename T> class TemplateClass
The code below does not compile in Visual C++ 2005. class SomeClass { public:
The Below code does not fail to compile but however at runtime it says
The code below does not compile. The compiler error is Cannot implicitly convert type
I have a simple example below that does not compile. i get the following
Does making the below class final (adding public final class) have any real impact
What does _currentHandle() mean below? template<class SpiHandleT> class SpiHandleIterator : public ISpiHandleIterator<SpiHandleT> { public:
When I try to execute the below class using ICriteria, if (_userGroupId > 0
I can't understand how works the code below: class Host: name = None fileList
If you look at the method below you will se a declared local class

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.