Scala: Can I declare a public field that will not generate getters and setters when compiled?
scala
Solution
The generated class does not contain getters or setters as can be seen in the sample you provided. The generated classes does not contain java bean getters or setters. To actually make the compiler generate `getX` and `setX` methods for a `var` you need to annotate that variable with `@BeanProperty`.
If you would like to have a public field accessible from java I think you are out of luck unfortunately. Atleast, I have not seen a way to accomplish that only using scala.
You could accomplish it by mixing scala and java. With a java class like:
public abstract class JavaClassWithPublicField {
public String name = "My name";
}
And then in your scala code inherit that class:
class ScalaClassWithPubilcField extends JavaClassWithPublicField
That's likely the cleanest way to do it.
Problem
In Scala when you declare a val or a var, Scala will automatically generate a private field and the getters and setters for you when compiled to bytecode. E.g. ``` class myClass { val name = "My Name" } ``` will compile to create the equivalent ``` class myClass { private string name; public string name(); public void name_$eq(string); } ``` Where name() and name_$eq are the getters and setters for the private string name. I know you can force it to not provide the getters and setters for private fields by declaring them as private[this] val/var blah, but I need to be able to create a public field that doesn't generate getters and setters when compiled. Is this even possible in Scala? Thanks