DEV Community

[Comment from a deleted post]
Collapse
 
scottcarey profile image
Scott Carey

Java 10 also allows you to write var list = List.of("Carlos", "Joseph", "Terry", "Katherine"); when inside a method ('var' is not allowed on members).

Arrays.asList returns an ArrayList, so why wrap?

Here is a singleton in Java:

enum Foo {
  INSTANCE;
}

and if you're mutating state in a singleton, in Kotlin or Java, you're doing it wrong.

Java is far too verbose for lots of things for sure. Especially for data class type use cases. But it is much better now than it was 5 years ago. Data class like things are on the way, though they probably won't be here for 2 years.

 
chrisvasqm profile image
Christian Vasquez • Edited

Thanks for the tips Scott,

Regarding the Singleton:

"and if you're mutating state in a singleton, in Kotlin or Java, you're doing it wrong."

Could you make an example with a real life scenario?

I had the idea that config files or something like a ShoppingCart as a Singleton is OK.

So taking the ShoppingCart as an example, I guess it would have a List<Product> products property that allows users to add() and remove() products from it. What could be an alternative?