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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:28:30+00:00 2026-06-05T07:28:30+00:00

I would like to create a Java class that follows the Scala setters/getters convention.

  • 0

I would like to create a Java class that follows the Scala setters/getters convention.

I tried following simple class, but it does not work:

public class JavaA {
private int a = 0;

public int a() {
    return a;
}

public void a_$eq(int a) {
    this.a = a;
}
}

But when I try to access it from scala:

val x = new JavaA
x.a = 1

and I get “reassignment to val” error message. I tried to look for this, but all issues I found where the other way around from scala to java.

What is the right way to do it?

Thanks!

  • 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-05T07:28:31+00:00Added an answer on June 5, 2026 at 7:28 am

    You can only sort of do this, and it’s hard enough that you probably don’t want to.

    What you can’t do is write a bare Java class that magically is interpreted as Scala getters and setters. The reason is that Scala embeds information into the class file that it requires for its getters and setters (e.g. are there zero parameter blocks or one empty parameter block–a distinction which is not preserved on the JVM (or in Java)).

    What you can do is use Java to implement a Scala-defined interface (i.e. trait):

    // GetSetA.scala
    trait GetSetA { def a: Int; def a_=(a: Int): Unit }
    
    // JavaUsesGSA.java
    public class JavaUsesGSA implements GetSetA {
      private int a = 0;
      public int a() { return a; }
      public void a_$eq(int a) { this.a = a; }
    }
    

    What you can’t do, even so, is use the class directly (again because Java does not add the appropriate annotation information for Scala):

    scala> j.a = 5
    <console>:8: error: reassignment to val
           j.a = 5
    

    but since it does implement the trait successfully, you can use it as desired when it is typed as the trait:

    scala> (j: GetSetA).a = 5
    (j: GetSetA).a: Int = 5
    

    So it’s rather a mixed bag. Not perfect by any means, but it may be sufficiently functional to help out in some cases.

    (The other alternative, of course, is to provide an implicit conversion from the Java class to one that has a getter/setter that references the real methods on the Java class; this works even when you can’t have the Java inherit from Scala.)

    (Edit: Of course there’s no critical reason that the compiler must act this way; one could argue that interpreting Java-defined getter/setter pairs as if they were Scala ones (i.e. if the classfile does not explicitly say it’s from Scala) is a good candidate for a feature enhancement to improve Java interoperability.)

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

Sidebar

Related Questions

I would like to create a simple XMPP client in java that shares his
I would like to create an initialisation method for a Java class that accepts
I would like to create a JTable like the below picture: Which java class
I would like to use a Swing Timer , in a Java class that
I am trying to create a class say MyStack that would implement a java.util.collections
I would like to create a map that uses a class as a key
I would like to create a base class that all classes in my program
I would like to create a java List based on another java Collection eg.
I would like to create extend a Java Swing application to have a look
I would like to create an object of Generics Type in java. Please suggest

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.