Ray
Ray

Reputation: 480

does Java class have a default value?

Can a default property be set to a Java class?

This used to be a little trick in VB so my code would look like this;

Team.Score;

instead of

Team.Score.getScore();

It's hardly a show stopper but since this is my first Java application I had to wonder.

Upvotes: 0

Views: 92

Answers (1)

Peter Lawrey
Peter Lawrey

Reputation: 533442

There is no way to implicit call a getter or setter. In general, Java avoids any implicit behaviour (until Java 8) so you have to say what you want the code to do. The benefit of this is you can see much more clearly see the details of what the code is doing. The down side is that you can't hide details of what the code is doing so the author can highlight what the code is supposed to do so easily.

Your options are

Getting a value

  int x = myObject.my_value;  // get a field, not ideal as it break encapsulation
  int y = myObject.value();   // short getter style
  int z = myObject.getValue();// JavaBean getter style

For setters, you options are

  MyObject(int value);        // only set it in the constructor
  myObject.m_value = x;       // not ideal again
  myObject.value(y);          // short setter style
  myObject.setValue(z);       // JavaBean setter

Upvotes: 2

Related Questions