-4

I was studying in a book and here is the example:

public class SimpleGeometricObject {
private String color = "white";
...

I've always been taught that we can't give a value to a member variable if its not in a constructor or in a setter. This example from the book, is it a good example? Can we do that? Is that a style problem?

Thanks

user258813
  • 57
  • 7

1 Answers1

0

You can initialize the variable either inside the constructor or outside, both have their benefits and drawbacks. There are a few reasons as explained in the following threads:

Community
  • 1
  • 1
dat3450
  • 954
  • 3
  • 13
  • 27