mu_sa
mu_sa

Reputation: 2725

Primitive and Non Primitive types getters/setters and initialization

Are Java non-primitive (object) data types treated in the same way as primitive data types? By treatment I mean...

1 : Should they be private in the class?

2 : Should they be initialized to some value during the declaration? (e.g. String S = "" instead of String S)

3 : Should they have getters and setters too?

Upvotes: 4

Views: 4703

Answers (8)

Edwin Dalorzo
Edwin Dalorzo

Reputation: 78579

I am not aware of any rule that says any particular attribute should be private in a class, Java offers other modifiers like protected and public, and no modifier which implies package accessibility. These all are there so that you can enforce the different levels of encapsulation that you may consider appropriate according to your design.

On the field initialization part of the question, I think that when a field is declared in a class, if the field is of any reference type it is by default initialized to null, so you do not need to initialize it unless you consider it necessary or unless the field is declared as final which implies you want to initialize it to a default value.

About the getter and setter part of the question, I believe these are just a way to enforce encapsulation. Allan Snyder in his paper Encapsulation and Inheritance in Object-Oriented Programming Languages wrote:

To maximize the advantages of encapsulation, one should minimize exposure of implementation details in external interfaces [...] For example, one characteristic of object-oriented language is whether it permits a designer to define a class such that its instance variables can be renamed without affecting clients.

Also the great article by Leonid Mikhajlov and Emil Sekerinski caled A Study of the Fragile Base Class Problem may demonstrate some good ideas on why all these levels of encapsulation and indirection are appropriate to avoid some classical problems related to inheritance.

"No direct access to the base class state" requirement: An extension class should no access the state of its base class directly, but only through calling base class methods.

Their paper demonstrates very compelling reasons on why something like getter and setter methods could be a good idea to avoid class extension fragility.

Upvotes: 3

guitarflow
guitarflow

Reputation: 2970

to 1 : of course, that's the principle of information hiding

to 2 : Many of them are already initialized "under the hood", but you should better do it yourself

to 3 : of course, also information hiding

Best, Flo

Upvotes: 4

yiannis
yiannis

Reputation: 1441

Regarding 1. and 3. the general principle is data encapsulation and it's not really related to the type of the data (primitive or non-primitive).

Regarding initialisation: class reference variables are by default initialised to null. Other than that, they should simply be initialised to whatever value suits the design of you class.

Upvotes: 1

NPE
NPE

Reputation: 500317

The simple answer to all three questions is "yes". The three practices are sound irrespective of whether the variable in question is of a primitive type.

One gotcha to be aware of is that, if you have a reference to a mutable object, and your getter returns that reference, this potentially permits the caller to modify the object (which you thought was private to your class!)

Another difference between the two cases is that object references can be set to null. This can be useful, but can also be a source of errors. It is perhaps worth pointing out here that String s = null and String s = "" are not the same.

Finally, with objects it is important to understand the difference between obj1 == obj2 and obj1.equals(obj2). This distinction doesn't arise with primitive types, since there's only one way to compare them for equality.

Upvotes: 4

Uzair
Uzair

Reputation: 53

1 : Should they be private in the class. YES 2 : Should they be initialized some value during the declaration (for eg : String S = "" instead of String S) YES 3 : Should have the getters and setters too. YES

Upvotes: 2

loveToCode
loveToCode

Reputation: 139

Definetly, object type variables should be private, initialized and have getter and setters as appropriate. This allows OOP to keep our code secure but still accessible.

Upvotes: 2

Stephen C
Stephen C

Reputation: 718788

1 : Should they be private in the class.

Yes. For precisely the reasons that you should declare primitive-typed variables to be private.

2 : Should they be initialized some value during the declaration (for eg : String S = "" instead of String S)

That depends. If the uninitialized state (null) has an application-specific meaning, then you shouldn't initialize by default. Otherwise, it is best to initialize to a non-null default value, if possible.

3 : Should have the getters and setters too.

Yes. For precisely the reasons that you should declare getters in the primitive-typed case. You should also declare setters, but only if you want the variable to be settable.

Upvotes: 2

AlexR
AlexR

Reputation: 115328

The answer to all question is "yes". You are describing here the common principal of data encapsulation and bean naming convention that is type independent.

Upvotes: 2

Related Questions