11

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.

14
  • 3
    Make it private. Make it final. Commented Mar 28, 2013 at 12:47
  • 1
    What Sayem said. Public member variables are "considered harmful." When your object doesn't change (we say it is "immutable"), declare the member variable final. This forces you to set it in the constructor and prevents it from changing -- even if someone reflects your class instance. Commented Mar 28, 2013 at 12:49
  • That was also what I thought would be the better option. What is your reasoning? Commented Mar 28, 2013 at 12:49
  • 6
    As the Gandalf said to Frodo: "Keep it secret, keep it safe" Commented Mar 28, 2013 at 12:50
  • private final BLABLABLA; Commented Mar 28, 2013 at 12:50

8 Answers 8

8

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.

Sign up to request clarification or add additional context in comments.

3 Comments

I have already learned about encapsulation. I thought it was weird that they tried to teach me to make it public at my internship. I will still do it for their product, because they do it themselves. However I would never do it for my own projects.
@WereWolfBoy: Yes, in production quality code you should always try to use private data members. And yes, it was weird that they tried to teach you while you were doing intern (I mean you are supposed to learn how to write production quality code while doing intern, right?). It's however OK to make it public in your test/play/pet projects, for example, but it's never OK in the production quality code. May be you should ask them about this......
I would say that this person is a hacker, not a developer, and probably doesn't understand all the ramifications of his decission. And, having seen his advice, I would be weary about anything he/she told me in the future. But, keep in mind, as an intern, they will treat you like you don't know what you are talking about, so don't try to argue the point.
4

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.

Comments

1

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

Comments

1

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.

Comments

1

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.

Comments

1

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.

Comments

1

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.

1 Comment

It is true that a lot of the things they make are website related... containing lots of javascript code. So it's probably because of that that they use it in java/android as well.
0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.