Do we have a Readonly field in java (which is set-able within the scope of the class itself)?

拥有回忆 提交于 2019-12-03 06:30:33

问题


How can we have a variable that is writable within the class but only "readable" outside it?

For example, instead of having to do this:

Class C {
  private int width, height;

  int GetWidth(){
    return width;
  }

  int GetHeight(){
    return height;
  }

  // etc..

I would like to do something like this:

Class C {
  public_readonly int width, height;

  // etc...

What's the best solution?


回答1:


There's no way to do this in Java.

Your two options (one which you mentioned) are using public getters and making the field private, or thorough documentation in the class.

The overhead on getter methods in extremely small (if at all). If you're doing it a large number of times, you might want to cache the fetched value instead of calling the get method.

EDIT:

One way to do it, although it has even more overhead than the getter, is defining a public inner class (let's call it innerC) with a constructor that is only available to your C class, and make your fields public. That way, you can't create innerC instances outside your class so changing your fields from outside is impossible, yet you can change them from inside. You could however read them from the outside.




回答2:


Create a class with public final fields. Provide constructor where those fields would be initialized. This way your class will be immutable, but you won't have an overhead on accessing the values from the outside. For example:

public class ShortCalendar
{
    public final int year, month, day;

    public ShortCalendar(Calendar calendar)
    {
        if (null == calendar)
            throw new IllegalArgumentException();

        year = calendar.get(Calendar.YEAR);
        month = calendar.get(Calendar.MONTH);
        day = calendar.get(Calendar.DATE);
    }
}



回答3:


from what I know the compiler does not optimize this kind of code

The Hotspot JIT compiler most definitely does.

I was wondering what's the best solution?

Stop optimizing prematurely. If you're using this code in a painting routine, I can guarantee that the actual painting will take at least a hundred times longer than calling a trivial method, even if it's not inlined.




回答4:


there is no way to make a field "read only" from outside. the only - and right - way is to make the fields private and provide only getters, no setters.




回答5:


Actually the HotSpot compiler would most likely inline calls to your getters, so no overhead will be involved (besides, the overhead for calling these methods would hardly be measurable).

EDIT

If you really need every CPU cycle, use C or C++ (or write performance-critical parts in it and call it via JNA, though its unlikely that it will be worth the time spent).




回答6:


Your solution is: private fields, private setters, protected or public getters (note: protected allows access from same package as well as from subclasses)




回答7:


yes.. we can make a variable read-only using final keyword

Ex: public final int id = 10;



来源:https://stackoverflow.com/questions/8151342/do-we-have-a-readonly-field-in-java-which-is-set-able-within-the-scope-of-the-c

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!