Final variable manipulation in Java

前端 未结 11 699
无人及你
无人及你 2020-11-28 23:28

Could anyone please tell me what is the meaning of the following line in context of Java:

final variable can still be manipulated unless it\'s immut

相关标签:
11条回答
  • 2020-11-28 23:44

    If you have a final reference to a Java object you can still manipulate it but cannot change its reference. For instance this code is perfectly legal:

    import javax.swing.JLabel;
    
    class Test1 {
        private final static JLabel l = new JLabel("Old text");
        public static void main(String[] args) {
            System.err.println(l.getText());
            l.setText("New Text");
            System.err.println(l.getText());
        }
    }
    

    But you can't say:

    l = new JLabel("Newest Text");
    

    After the first assignment to l. Note that you can do this though:

    import javax.swing.JLabel;
    
    class Test1 {
        public static void main(String[] args) {
            final JLabel l;
            String s = getArbitaryString(); // Assume this method returns a string
            l = new JLabel(s);
            System.err.println(l.getText());
        }
    }
    

    This can be done because when l is declared it is not assigned to anything not even null. So you are allowed to assign something to it one time only.

    Same thing goes for primitives. You can assign a value to it like this:

    class Test1 {
        public static void main(String[] args) {
            final int i;
            i = 2;
        }
    }
    

    But now you cannot manipulate it further since the only thing you can do to primitive types is to assign values to them.

    0 讨论(0)
  • 2020-11-28 23:44

    There two things Final variable and final reference variable.

    If we are using final keyword with primitive data type we can't change anything. But if we are using final keywprd with non - primitive data, we can change it's properties like:

    1. If you are using a final keyword with primitive types of the variable (int, float, char, etc) then you can’t change the value of a final variable once it is initialized. So, we should initialize it.

    2. If you are using a final keyword with non-primitive variables (By means of non-primitive variables are always references to objects in Java), the member of the object can be changed. It means we can change the properties of the object, but we can’t change to refer to any other object.

    https://javagoal.com/final-keyword-in-java/

    0 讨论(0)
  • 2020-11-28 23:45

    It means that if your final variable is a reference type (i.e. not a primitive like int), then it's only the reference that cannot be changed. It cannot be made to refer to a different object, but the fields of the object it refers to can still be changed, if the class allows it. For example:

    final StringBuffer s = new StringBuffer();
    

    The content of the StringBuffer can still be changed arbitrarily:

    s.append("something");
    

    But you cannot say:

    s = null;
    

    or

    s = anotherBuffer;
    

    On the other hand:

    final String s = "";
    

    Strings are immutable - there simply isn't any method that would enable you to change a String (unless you use Reflection - and go to hell).

    0 讨论(0)
  • 2020-11-28 23:45

    You can manipulate mutable final variables for e.g. of type StringBuffer but you cannot manipulate final variables of immutable types.

    In case of mutable variables, new object is not created every time it's value is changed. But in case of of immutable types, whenever you change value, new object is created, so when you make it final, you cannot modify it.

    0 讨论(0)
  • 2020-11-28 23:53

    You can still change a 'final' variable using Reflection.

    0 讨论(0)
  • 2020-11-28 23:55

    You can call any method on it even if the method can change the state of the object the reference is pointing to. E.g

    final MyClass myClass = new MyClass();
    myClass.setVar(something);
    

    This is fine because myClass itself is not changing, i.e you are not doing myClass = myClass1;.

    0 讨论(0)
提交回复
热议问题