me_digvijay
me_digvijay

Reputation: 5492

Making String private or public in Java

Strings in Java are said to be immutable. So if I say

String one = "myString";

and

String two = "myString";

Internally both the objects will be using the same literal. Now what puzzlles me is why should I make my Strings private in different classes, when internally they will be referencing to the same string literal.

Is it just that external elements(like classes or object) will not know what members the class has inside it?

Upvotes: 1

Views: 11306

Answers (4)

Madhujith
Madhujith

Reputation: 157

Using private for String reference blocks the direct access from outer objects. But setters and getters allow you to access/change the string variable (if you set). But in the setter method you can validate the parameters before changing the value. And that's the main advantage of using the setter method.

then, assume you have two equal private string variables in two objects. Then these two references should point to one place in the string pool. But that not sounds you can access both strings from one object (Because you always use reference variable that restricted by private modifier).

Upvotes: 0

Vidya
Vidya

Reputation: 30300

Because the client could then set your internal members to something else entirely. It seems like you might be confusing a String's being immutable with the reference being final.

Imagine you have this class:

public class MyClass {
    public String first = "Test";
    public String second = "Test";
}

So as Java compiler experts, we know internally first and second refer to the same object. Cool.

Now when I use MyClass:

MyClass myClass = new MyClass();
myClass.second = "Time to do some hackin'";

That's bad. Like real bad.

Declaring String members private has nothing to do with losing the advantage of compiler efficiencies. It is about the class encapsulating its implementation so that its clients aren't tightly coupled to it and so the class doesn't have to worry about weird behavior caused by something irresponsible the client did.

The fact that the object "Test" is immutable doesn't mean I can't change first or second to point to something else.

Hope that helps.

Upvotes: 0

Sotirios Delimanolis
Sotirios Delimanolis

Reputation: 279900

For String references, it doesn't really change anything. It's just a matter of convention and encapsulation. If your other fields are private and have getters and setters, why shouldn't your String fields (unless it's meant to be hidden)?

Upvotes: 0

Iłya Bursov
Iłya Bursov

Reputation: 24146

Visibility and access rights exists actually only during compilation, to check whether you're able to do this or that, during runtime generally you can call private methods or change immutable data via different hacks

Another point: these strings are immutable, so you cannot change their value via standard methods, so they can be the same place in memory and its doesnt matter which class uses them

Upvotes: 1

Related Questions