Hey there,
I have the following code:

    int[] x = new int[7];
    System.out.println(x.lengnth);
    x.length = 10;
    System.out.println(x.length);

Of course, line three causes a "cannot assign a value to final variable length". However, line one does assign seven to that variable, length.

Now, I have written a Testing class where I have declared a final variable of type String. I tried changing it either via the constructor or via a setter method and they both failed.

Now, my question is: how is length being set when neither a constructor nor a setter method can do this?

Another question would be:
In the Spring framework, when doing dependency injection, even if a variable is private and there is no corresponding setter method, the variable will be changed upon initialization. How is that happening?

Thank you!

Recommended Answers

All 7 Replies

Array is a bad example because arrays in Java receive special treatment. Arrays are objects but you'll never see the source code for array of Object or array of primitives in the source distribution. They are dynamically created. For more gory details, read this

However, line one does assign seven to that variable, length.

final doesn't mean that it can never be assigned. final means something which has to be assigned once and only once.

how is length being set when neither a constructor nor a setter method can do this

As already mentioned, arrays receive special treatment in the JVM to the point that they have special opcodes assigned for creating arrays. Thus, when you attempt to create an array of given size, that size is set to the length field using the invisible constructor dynamically generated by the JVM (as mentioned above).

In the Spring framework, when doing dependency injection, even if a variable is private and there is no corresponding setter method, the variable will be changed upon initialization

Maybe I'm reading your question wrong, but a private field doesn't mean it can never be initialized by outside code. If you class has a constructor which takes care of initializing private and final fields, the outside code can simply call the constructor and the private variable will be initialized even when the outside code can't directly access the class privates.

Spring has two forms of injection: setter and constructor. If your class doesn't have setters for a given private variable, it has to have a constructor which is capable of setting a given private variable.

s.o.s, thank you for your explanations.
However, this remains unclear:

public class Testing
{

    public final String s = null;

    public Testing(String s)
    {
        this.s = s;
    }
}

The class won't compile?!

The class won't compile?!

Please post the full text of the error messages.

The class won't compile?!

Because you have already assigned null once to the field s. The correct way is to do:

public class Test {
  private final String s;
  public Test(final String s) {
    this.s = s;
  }
}

Ok, I see. So, once a final variable already hass a value assigned to it, it may not be changed any longer. I think I got it now.

As far as Spring goes, I think one can assign a value to a private variable without either a constructor or a setter method. But I do not know how this is done. Any idea?

But I do not know how this is done. Any idea?

I think I know what you are talking of. There is no way of setting a private field without constructor or setter injection unless you are OK with using reflection to set your fields. Look into the @Inject annotation which does exactly this. It has been some time since I have been actively involved with Spring so look at the Spring docs for details on this.

Thank you s.o.s, have a good night.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.