Yosuo
Yosuo

Reputation: 13

What is good practice to accessing package private variables?

What is good practice to access my package-private variables from other classes in this same package?

  1. Package-private accessor

    String getColor() {
        return color;
    }
    
  2. Just accessing as field from object.

    String color = instanceOfClass.color;
    

In my opinion:

  1. Package-private method for accessing package-private field. A lot of unnecessary code, but in my opinion provides clarity with a lot of fields (and properly named accessor methods)

  2. We don't need accessors and mutators for package-private variables in package, so maybe I shouldn't create them?

Which practice is better, consistent with the programming convention?

EDIT: Thank you for fast answers! :)

Upvotes: 1

Views: 2232

Answers (4)

bhagat
bhagat

Reputation: 163

Best practice is to use Getters and Setters.

This allows additional functionality (like validation) to be added more easily later and prevents you from giving the privilage of directly accessing these variables from other class.

private String color = InstanceOfClass.color;

String getColor(){
    if(background == White)
        return Black;

    else
        return White;

    }

You can have a good information about them here: Getter Setter: To Use or Not to Use

Upvotes: 0

Vojtech Ruzicka
Vojtech Ruzicka

Reputation: 17075

Having accessors/mutators is usually handy even if it is some more code:

  • If you later introduce some logic when accessing/setting the variable, you can do it just in one place without the need to affect all the classes using the variable - you can easily add features as additional logging, lazy loading, security, validation, ...

  • You can later change the underlying representation of the field (eg return subtype in some cases)

  • You can later introduce lifecycle management of the returned object - eg. Return pooled/cached/singleton/.. object
  • You can later decide to return Proxy/decorated object instead without affecting callers

Generally, it is a good idea as it gives you more flexibility, preserves encapsulation and reduces coupling between objects.

Upvotes: 1

notphilphil
notphilphil

Reputation: 385

Use accessors only when necessary. Other than that, it is best to only allow package private variables to be accessed inside of that package. For example, if the variable is some value used in a calculation, do what you need with it inside the package using a public method call from another package, and then pass it to your other package.

Upvotes: 0

christopher
christopher

Reputation: 27336

Accessors and mutators have more to do with abstraction than they do with encapsulation. You want to be able to control how that field is set, rather than letting anyone put some random value in there. For example, you might have a value that should never be null, so..

void setField(Field field) {
    if(field != null) {
        this.field = field;
    }
}

And then you would declare your field variable as private. If you allow anyone to write anything into it, then this logic would need to be moved to everywhere that sets it, which will cause code duplication.

Upvotes: 3

Related Questions