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 6621275
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T21:14:30+00:00 2026-05-25T21:14:30+00:00

In Java, when using an object across multiple threads (and in general), it is

  • 0

In Java, when using an object across multiple threads (and in general), it is good practice to make fields final. For example,

public class ShareMe {
    private final MyObject obj;
    public ShareMe(MyObject obj) {
        this.obj = obj;
    }
}

In this case, the visibility of obj will be consistent across multiple threads (let’s assume obj has all final fields as well) since it is safely constructed using the final keyword.

In scala, it doesn’t appear val compiles down to a final reference, but rather val is semantics in scala that prevents you from reassigning a variable (Scala final variables in constructor). If scala constructor variables are not defined as final, will they suffer from the same problem (when using these objects in actors)?

  • 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-05-25T21:14:31+00:00Added an answer on May 25, 2026 at 9:14 pm

    The answer to the other question is misleading. There are two meanings of the term final: a) for Scala fields/methods and Java methods it means “cannot be overridden in a subclass” and b) for Java fields and in JVM bytecode it means “the field must be initialized in the constructor and cannot be reassigned”.

    Class parameters marked with val (or, equivalently, case class parameters without a modifier) are indeed final in second sense, and hence thread safe.

    Here’s proof:

    scala>  class A(val a: Any); class B(final val b: Any); class C(var c: Any)
    defined class A
    defined class B
    defined class C
    
    scala> import java.lang.reflect._
    import java.lang.reflect._
    
    scala> def isFinal(cls: Class[_], fieldName: String) = {
         |   val f = cls.getDeclaredFields.find(_.getName == fieldName).get
         |   val mods = f.getModifiers
         |   Modifier.isFinal(mods)
         | }
    isFinal: (cls: Class[_], fieldName: String)Boolean
    
    scala> isFinal(classOf[A], "a")
    res32: Boolean = true
    
    scala> isFinal(classOf[B], "b")
    res33: Boolean = true
    
    scala> isFinal(classOf[C], "c")
    res34: Boolean = false
    

    Or with javap, which can be conveniently run from the REPL:

    scala> class A(val a: Any)
    defined class A
    
    scala> :javap -private A
    Compiled from "<console>"
    public class A extends java.lang.Object implements scala.ScalaObject{
        private final java.lang.Object a;
        public java.lang.Object a();
        public A(java.lang.Object);
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using Java's Transformer class to process an XML Document object. This is the
Is there a way to mock object construction using JMock in Java? For example,
By using java reflection, we can easily know if an object is an array.
The Java doc describe that the constructor Date(long date) constructs a Date object using
I have a C program that stores some object in java store using JNI.
I'm thinking about using the java application object to implement a simple cache, to
I'm having a problem using the java.text.MessageFormat object. I'm trying to create SQL insert
I'm learning Java here, and using a GLabel object. It's in the ACM Graphics
I want to inject a plain java object using Spring programmatically without using any
am sending one user object from java to flex using remote object,now i want

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.