Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Public property VS Private property with getter?

This question has puzzled me for a while. A public property that can be accessed directly or a private property with getter? Which one is better/correct and why?

like image 837
Pouya Avatar asked Nov 03 '11 22:11

Pouya


People also ask

Should getters be public or private?

In general, they should be public. If they are private they can only be called from within your class and, since you already have access to the private variables within your class, are redundant. The point of them is to allow access to these variables to other, outside, objects.

Should getters and setters be public or private?

Usually you want setters/getters to be public, because that's what they are for: giving access to data, you don't want to give others direct access to because you don't want them to mess with your implementation dependent details - that's what encapsulation is about.

What is a getter property?

In JavaScript, accessor properties are methods that get or set the value of an object. For that, we use these two keywords: get - to define a getter method to get the property value. set - to define a setter method to set the property value.

What is the difference between private and public property?

Private property is a legal designation for the ownership of property by non-governmental legal entities. Private property is distinguishable from public property, which is owned by a state entity, and from collective or cooperative property, which is owned by a group of non-governmental entities.


4 Answers

Private properties with getters (and possibly setters) are considered proper style since declaring them public and using them directly harms the principle of encapsulation. One of the issues it can cause is that you rely directly on the implementation type of the field, making it harder to change later if the need should arise.

Besides, getters/setters allow you to add logic to the process of accessing and mutating. You could perform boundary checks, input validation, substituting nulls with default values...

Then again, in many cases, like most JavaBeans usage, the getters/setters do nothing more than what you'd do with direct access. So this entire thing is kind of contested in the JavaBeans context.

My opinion? The entire matter is severely overstated and the amount of discussion it has sparked has eaten enough time and keyboard strokes to create an entirely new Java Language Spec with proper properties. Don't listen to dogma, do what works best for you and never stop thinking about what does and doesn't make sense. If everybody just accepted the words from above, we'd probably still be coding in assembly.

like image 25
G_H Avatar answered Oct 13 '22 00:10

G_H


Exposing fields directly is considered a bad practice.

It is better to keep the field private and only expose the getter and setter. One advantage is that you can choose different access levels for the getter and setter, whereas a field has only a single access level. Another advantage of using getters is that it allows you to change the implementation without changing the class interface.

Even better is to avoid getters and setters where possible. Instead use methods that encapsulate a higher-level behaviour. This is because objects shouldn't be tampering with other objects' internal state (either via directly accessing fields, or indirectly via getters and setters).

Related

  • Are getters and setters poor design? Contradictory advice seen
like image 81
Mark Byers Avatar answered Oct 13 '22 02:10

Mark Byers


Generally it's preferred to use getters rather than public fields because making the field private and exposing a getter prevents callers from being able to modify the field, and allows you to change the implementation later without changing the interface.

One exception to this rule is for logical constants, for which people generally prefer public static final fields: Days.SATURDAY rather than days.getSaturday(), for instance. But if the value is attached to a particular instance rather than a class, or could ever change, or generally doesn't feel like it must be a universal constant, the flexibility provided by private field / public getter makes it preferable.

like image 1
jacobm Avatar answered Oct 13 '22 02:10

jacobm


Try always to declare fields as private. Suppose you have a bank account and you want to draw some cash:

public class BankAccountWithprivateField
{
    private Cash cash;

    // ...

    public void draw(int amount)
    {
        if(amount <= balance) cash.draw(amount);
        else sentMessage("Sorry your balance is less than what you request!");
    }
}

// ...

new BankAccountWithprivateField().draw(500);

Here you cannot draw more than what you have in your balance.

public class BankAccountWithPublicField
{
    public Cash cash;

    // ...
}

// ...

new BankAccountWithPublicField().cash.draw(1000000);

But here, you can draw 1 million dollar ;)

like image 1
Eng.Fouad Avatar answered Oct 13 '22 00:10

Eng.Fouad