guys.. me confused

Discussion in 'Plugin Development' started by Pizza371, Dec 22, 2013.

Thread Status:
Not open for further replies.
  1. Offline

    Pizza371

    Okay, so nothing big but I just found out you can use finals in enums and well.. i can modify them?
    Is there a reason for this? Otherwise whats the point in finals in enum ?
    I might just be derping, so..
    Thanks!
     
  2. Offline

    hellboyPS

    Can you give us an example? You can only not modify final references.
    For example, this works fine and as intended:
    Code:
    final int[] a = new int[]{1};
    a[0] = 2;
    
     
  3. Offline

    Pizza371

    private final String value;

    method() {
    value = "string";
    }

    why can I re-assign variables? I thought you couldnt do that, otherwise I might be understanding finals wrong..
    thanks! hellboyPS
     
  4. Offline

    Tirelessly

    Pizza371 that's not an enum, and you can't do that. In fact, that'd likely be a compiler error unless method was a constructor.
     
  5. Offline

    Pizza371

    Tirelessly i didn't think it was necessary to post Enums and yes it's a constructor (I didn't think it had any other abilities than other methods..) this is what I mean:

    Code:
    private final String value;
     
        private Msg(String value) {
            this.value = value;
        }
    EDIT: Thanks! whoops double tahg :p
     
  6. Offline

    Tirelessly

    Pizza371 That's no really reassigning it then, because you're only assigning it once. Trying to change the value again anywhere would be an error. And I bring up the enum thing because in your original post you said enums, which I think it's doing what you think it is (a final class cannot be extended)
     
  7. Offline

    hellboyPS

    Everything seems completely fine here.You are just assigning the variable the first time.
     
  8. Offline

    Pizza371

    Tirelessly hellboyPS thanks for the help guys, but why does this not work outside of an enum class?
    e.g.
    enum class:
    Code:
    private final String value;
    it is fine, I can re-assign the value at a later stage if I want (but only once as I tested, thanks to yous).
    class:
    Code:
    private final String value;
    (pasted)
    [​IMG] < eclipse IDE
    So, if I understand correctly:
    - finals can be added to classes to stop them from being extended (I knew this already, but thanks tirelessly)
    - finals can be used locally and re-assigned if the code is going to be executed within the same block
    - finals can be re-assigned once ONLY in the constructor, since constructor is always going to run when instantiating the class
    ...
    but they can't be re-assigned if wanting to be changed outside of a constructor?

    If you guys can be bothered to read all this, please point out if anything is wrong.

    much appreciated!
     
  9. Offline

    hellboyPS

    So the biggest issue in your posting is that you don't supply any actual code snippets.
    We can't really say anything, if we don't see your code and the context of it.

    Finals shouldn't make any difference if used in an enum or a class.
    Finals in a class declaration are different from finals in a variable or method declaration.
    Finals prevent reassigning of a variable. If you didn't assign the variable a value, you can just do so.
    But because your IDE has no way of knowing if the variable is assigned a value at runtime, you get this error. There is an exception in the constructor as the IDE can be sure that the variable is still emtpy.

    So you can assign the final variable a value once and this can be either at declaration or in a constractor. Nowhere else.
     
    Pizza371 likes this.
  10. Offline

    Tirelessly

    Just wanted to highlight this point Pizza371
     
    Pizza371 likes this.
  11. Offline

    Pizza371

    Tirelessly @hellyboyPS
    thank you, I understand now.

    hellboy:
    I didn't really think it mattered, it wasn't really a direct error, I was just very curious because I thought it was because of enum, not constructor.
    Anyway, I understand now.
     
Thread Status:
Not open for further replies.

Share This Page