WereWolfBoy
WereWolfBoy

Reputation: 508

private String or public static String?

At my internship one of my colleagues gave me a hint. I want to know if this is good practice.

What I was doing was creating classes that are used only for the values they contain and don't have any functions that actually do something (apart from having getters, setters and a constructor). I declared my variables like this:

public class ObjectIUse{
  Private String name;

  public ObjectIUse(String name){
    this.name = name;
  }

  public String getName(){
    return name;
  }
}

So I'm not using a setter because it should always stay the same. My colleague said that I can also do it this way:

public class ObjectIUse{
  public final String name;

  public ObjectIUse(String name){
    this.name = name;
  }
}

Because now we don't need to have any getters or setters because it is public, however it can also never be changed because it is final.

Which would be better? Or would it maybe be preferable to still make it private but also final? I mean all of the options work, obviously. I just want to know which is better and why.

Upvotes: 11

Views: 77544

Answers (8)

MD Sayem Ahmed
MD Sayem Ahmed

Reputation: 29176

Make the variable private, because by doing so you'll be encapsulating the variable in your class. This has many benefits, information hiding is among one, which you'll learn if you go to the above link.

If you want it to never change after creation, then make it final too.

Upvotes: 8

shapkin
shapkin

Reputation: 377

I think it's depends. For example: if you use getter - you can override it. Sometimes it very usefull.

Upvotes: 0

David Lavender
David Lavender

Reputation: 8331

I guess their reasoning is that having it public keeps the code simpler. Java gets criticised for being too verbose in situations like this. Over a language such as Javascript where this would (normally) always be public.

But that simplicity is a trade-off against having secure, stable and extendable code.

To see why that's important, take a look at a large-scale Javascript project that has been written with everything as public. Each class's code might be simple... but their relationships, and the resulting architecture, end up being a nightmare to maintain.

Upvotes: 1

Simon Dorociak
Simon Dorociak

Reputation: 33515

Which would be better? Or would it maybe be preferable to still make it private but also final?

If you want to be successful developer, you should program correctly, efficiently and the most important securely. Security and performance is on the first place.

When you make it public you will break encapsulation that is very important and has many benefits. Every time you want to get property of Object, getters will become your friend.

Generally you shouldn't have direct access to properties of Object(only in extrem cases but also these can be solved in better way). Getters and Setters are designated for these purposes - preserve encapsulation and deal with objects securely.

final variables are usually used for data which are unchangeable during a time.

Upvotes: 1

Deepak Bala
Deepak Bala

Reputation: 11185

This works now because a String is immutable. But what happens when you expose the reference to a mutable class and that class is not thread safe ?. You cant even return a defensive copy if you want to.

Not to mention this also breaks encapsulation. Use a private variable and getters.

Upvotes: 4

Egor
Egor

Reputation: 40218

A proper use of encapsulation principle is to make all class fields private and access them via setters and getters. Other than that, you might want to add any additional logic when you're calling getName(). While second variant is sometimes used, the first one is better. Hope this helps.

Upvotes: 1

tibo
tibo

Reputation: 5494

You should definitely have a getter and make your field private. That is what we call encapsulation.

Also by making it final and so not having a setter, your object is immutable, whic is a very good thing for parallel programming.

Upvotes: 1

tmwanik
tmwanik

Reputation: 1661

The idea of not providing a setter method to a variable makes it a read-only field, that said, it means we can only read but not write, so making it a constant by the use of the final keyword summarizes it all.

I think a constant is better. final keyword improves performance. Read more here

Upvotes: 1

Related Questions