gstackoverflow
gstackoverflow

Reputation: 37034

Treatment to field inside the class. Through getter or explicit?

consider the class:

class MyClass{
   MyOtherClass obj;
   //setObj and getObj methods
   public void someMethod(){
      ... 
      //access to obj needs.
      ...    
   }
}

How to right replace

//access to obj needs.

through getter or explicitly?

P.S.

I saw both variants in my expirience.

Upvotes: 0

Views: 76

Answers (3)

Aleks
Aleks

Reputation: 5854

To answer this, let's first see why getters and setters were introduced in the first place. It is clear that direct access to data members is simpler.

SOme of the reasons are:

  • for a better encapsulation, to hide the property implementation from a class user. For example you can internally store a temperature value in C and return it by a getter in F.
  • for more control over the access. If you want to do something more besides pure getting/setting a piece of data, you would need a method. For example, you might want to log the change of value for audit purpose
  • methods are much more "interface friendly" than pure data members.

In this case the class itself accesses its own property. Are you sure you want that?

If so, let's see the reasons:

Encapsulation is definitelly not needed, since the class itself accesses its own attributes.

Do you need to somehow control access here? Do you need to do something else, besides get/set? Are there any other possible users of this class?

If all these answers are NO, ans especially if the only user of this class the mentioned method, then go for a simpler option and use direct access, without getters/setters.

If some of the answers is true, just make a simple trade-off and decide.

Upvotes: 1

T.J. Crowder
T.J. Crowder

Reputation: 1074335

So here we're talking about code in Class MyClass accessing information in an instance of MyOtherClass.

Typically you don't get a choice. If MyOtherClass exposes a getter for a data member, it's unlikely to also expose that data member. If it does (even if the data member is, say, protected but the accessor is public), the design is a bit questionable.

But if you do have the choice, I would use the getter, rather than the exposed data member. It's a bit subjective, but using data members rather than accessors more tightly binds the classes together. In my protected/public example, you'd have more work to do if for any reason you wanted to move MyClass to a different package.


It's worth noting that using the getter is not more expensive in performance terms with a decent JVM (such as the one from Sun). If the code becomes a performance "hotspot" for whatever reason (or possibly even if it doesn't), the JVM's JIT will convert the call to the getter into a direct access anyway (presuming it's a pure getter), so you get the benefit of abstraction at the coding/design-time without the function call overhead at runtime.

Upvotes: 1

schmop
schmop

Reputation: 1440

Personally I would say it depends on the level of "connection" between both classes. If they are in the same package and part of the same "mecanism" (one would have no reason to exist without the other), bypassing accessors is acceptable.

Upvotes: 1

Related Questions