Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I use POJO equivalents in Scala?

Tags:

Suppose I have the following

class SimpleClass (myInt: Int, myString: String) {  } 

What is wrong with the following?

val mySimple = new SimpleClass(1, "hi") println(mySimple.myInt) 
like image 423
deltanovember Avatar asked Jul 17 '11 09:07

deltanovember


People also ask

How do I create a class object in Scala?

In Scala, an object of a class is created using the new keyword. The syntax of creating object in Scala is: Syntax: var obj = new Dog();

What is object vs class in Scala?

Difference Between Scala Classes and Objects Definition: A class is defined with the class keyword while an object is defined using the object keyword. Also, whereas a class can take parameters, an object can't take any parameter. Instantiation: To instantiate a regular class, we use the new keyword.

What is POJO explain with example?

POJO stands for Plain Old Java Object. It is an ordinary Java object, not bound by any special restriction other than those forced by the Java Language Specification and not requiring any classpath. POJOs are used for increasing the readability and re-usability of a program.


1 Answers

If you want the contructor parameters to be available as fields of the class, you have to declare them as vals or vars:

class SimpleClass (val myInt: Int, val myString: String) {  } 
like image 107
Kim Stebel Avatar answered Sep 24 '22 04:09

Kim Stebel