Search code examples
scalapojo

How to create POJO class with only empty constructor in Scala?


I want to create POJO class in Scala with only default empty constructor. In java, it's like this:

public class Foo {
    private String name;

    private String address;

    ...

    //and the public getter/setter below...
}

In scala, I have seen that you can create POJO like this:

case class Foo(var name: String, var address: String, ...)

But in my case, the class will have many properties (around 50+), and I don't think instantiating the class with 50 constructor parameters is fit for this case.

UPDATE:

Also, the class's properties value can be set (it's not read-only). This is how I expect the usasge of the POJO class:

val foo = new Foo()
foo.name = "scala johnson"
foo.address = "in my sweeet dream, oh yeah"
...

Solution

  • How about :

    class C {
      var p1:String = _
      var p2:Int = _
    }