简体   繁体   English

在Java中将String私有或公开

[英]Making String private or public in Java

Strings in Java are said to be immutable. 据说Java中的字符串是不可变的。 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? 仅仅是外部元素(如类或对象)将不知道类内部有哪些成员?

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 可见性和访问权限实际上仅在编译期间存在,以检查您是否能够执行此操作,通常在运行时期间,您可以调用私有方法或通过不同的hack更改不可变数据

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 还有一点:这些字符串是不可变的,因此您无法通过标准方法更改它们的值,因此它们可以在内存中位于同一位置,并且哪个类使用它们都无关紧要

For String references, it doesn't really change anything. 对于String引用,它并没有真正改变任何东西。 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)? 如果您的其他字段是私有字段,并且具有getter和setter方法,那么为什么不应该使用您的String字段(除非要隐藏它)?

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 . 似乎您可能将String的不可变与引用的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. 因此,作为Java编译器专家,我们在内部知道firstsecond引用同一对象。 Cool. 凉。

Now when I use MyClass : 现在,当我使用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. String成员声明为private与失去编译器效率的优势无关。 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. 事实上,对象“测试”是不可改变的,并不意味着我不能改变firstsecond点别的东西。

Hope that helps. 希望能有所帮助。

Using private for String reference blocks the direct access from outer objects. 对字符串引用使用private会阻止外部对象的直接访问。 But setters and getters allow you to access/change the string variable (if you set). 但是,使用setter和getter可以访问/更改字符串变量(如果已设置)。 But in the setter method you can validate the parameters before changing the value. 但是在setter方法中,您可以在更改值之前验证参数。 And that's the main advantage of using the setter method. 这就是使用setter方法的主要优点。

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). 但这听起来不行,您可以从一个对象访问两个字符串(因为您始终使用受private修饰符限制的引用变量)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM