<div dir="auto">Corrections, learn something new daily!</div><div dir="auto"><br></div><div dir="auto">If you google “immutable arrays in Java” the ai overview is good. Make a copy when returning, make final variables private. That’s the easy solution.</div><div dir="auto"> </div><div dir="auto">Also, the AI says:</div><div dir="auto"><br></div><div dir="auto"><div dir="auto"> </div></div><div dir="auto"> import java.util.Arrays;<br> import java.util.Collections;<br> import java.util.List;<br><br>/* [ editor’s note ] I doubt this is feasible, unless we hide stuff in a private constructor and call the private constructor with this(…) at the beginning ordinary constructors. Maybe worth a try, but likely the stylesheet would be rough. It’s probably easier to return a copy. But perhaps you could do the below when initializing a final List? Worth a try? I’m not familiar with most of the stylesheet I’m not sure if List can be passed to equals()???*/</div><div dir="auto"><br></div><div dir="auto"> public class MyClass {<br> private final List<Integer> immutableList;<br><br> public MyClass(int[] initialArray) {<br> this.immutableList = Collections.unmodifiableList(Arrays.asList(Arrays.stream(initialArray).boxed().toArray(Integer[]::new)));<br> }<br><br> public List<Integer> getImmutableList() {<br> return immutableList;<br> }<br> }<br></div><div dir="auto"><div dir="auto"><span style="font-family:monospace;font-size:18.199999px;font-style:normal;font-weight:400;letter-spacing:normal;text-indent:0px;text-transform:none;white-space:pre;word-spacing:0px;text-decoration:none;color:rgb(0,29,53)"></span></div><br></div><div dir="auto">Reference to documentation: <div><a href="https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-">https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-</a></div><div dir="auto"><br></div><div dir="auto">There are other unmodifiable collections.</div><div dir="auto"><br></div><div dir="auto">I’ll probably mess with my OArray.java to try this out, interesting!</div><br><div class="gmail_quote gmail_quote_container"><div dir="ltr" class="gmail_attr">On Tue, Jul 29, 2025 at 11:55 PM John Carlson <<a href="mailto:yottzumm@gmail.com">yottzumm@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-style:solid;padding-left:1ex;border-left-color:rgb(204,204,204)"><div dir="ltr">Apparently, the FIELDOFVIEW_DEFAULT_VALUE should be final (immutable). I think the story is, you can modify elements of an ArrayList<Float>, but you can't modify the variable.<div><br></div><div>The magenta? Looks wrong. A final variable got overwritten.</div><div><span style="color:rgb(0,0,0)">##########################################<br>((((getFieldOfView().length > 0) && !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)) || !ConfigurationProperties.getStripDefaultAttributes()) /* ArrayList .x3d compare */ && !hasUSE())=false<br>getFieldOfView().length > 0=true<br>!getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)=false<br>!ConfigurationProperties.getStripDefaultAttributes()=false<br>!hasUSE()=true<br></span><font style="color:rgb(0,0,0)">getFieldOfViewList()=[0.0, 0.0, 20.0, 20.0]</font><br><font style="color:rgb(255,0,255)">FIELDOFVIEW_DEFAULT_VALUE=[0.0, 0.0, 20.0, 20.0]</font><br><font style="color:rgb(0,0,0)">##############################</font><font style="color:rgb(0,0,0)">############</font></div><div><font style="color:rgb(0,0,0)"><br></font></div><div><font style="color:rgb(0,0,0)">Later, the magenta is correct. Why?</font></div><div><font style="color:rgb(0,0,0)"><br></font></div><div><span style="color:rgb(0,0,0)">##########################################<br>((((getFieldOfView().length > 0) && !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)) || !ConfigurationProperties.getStripDefaultAttributes()) /* ArrayList .x3d compare */ && !hasUSE())=false<br>getFieldOfView().length > 0=true<br>!getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)=false<br>!ConfigurationProperties.getStripDefaultAttributes()=false<br>!hasUSE()=true<br></span><font style="color:rgb(0,0,0)">getFieldOfViewList()=[-1.0, -1.0, 1.0, 1.0]</font><br><font style="color:rgb(255,0,255)">FIELDOFVIEW_DEFAULT_VALUE=[-1.0, -1.0, 1.0, 1.0]</font><br><font style="color:rgb(0,0,0)">##############################</font><font style="color:rgb(0,0,0)">############</font></div><div><font style="color:rgb(0,0,0)"><br></font></div><div><font style="color:rgb(0,0,0)">Here's the definition of the variable:</font></div><div><br></div><div>/** MFFloat field named <i>fieldOfView</i> has default value <i>{-1f,-1f,1f,1f}</i> (Java syntax) or <i>-1 -1 1 1</i> (XML syntax). */<br>public static final ArrayList<Float> FIELDOFVIEW_DEFAULT_VALUE = new ArrayList<>(Arrays.asList(-1f,-1f,1f,1f));</div><div><br></div><div>See example code to test (it gets even worse, even arrays themselves are mutable.</div><div><br></div><div>import java.util.ArrayList;<br>import java.util.Arrays;<br><br>public class OArray {<br> public static final ArrayList<Float> FIELDOFVIEW_DEFAULT_VALUE = new ArrayList<>(Arrays.asList(-1f,-1f,1f,1f));<br> public static final float[] ARRAY_FIELDOFVIEW_DEFAULT_VALUE = new float [] {-1f,-1f,1f,1f};<br> public static void main(String[] args) {<br> System.out.println(FIELDOFVIEW_DEFAULT_VALUE);<br> FIELDOFVIEW_DEFAULT_VALUE.set(0, 0f);<br> FIELDOFVIEW_DEFAULT_VALUE.set(1, 0f);<br> FIELDOFVIEW_DEFAULT_VALUE.set(2, 20f);<br> FIELDOFVIEW_DEFAULT_VALUE.set(3, 20f);<br> System.out.println(FIELDOFVIEW_DEFAULT_VALUE);<br> System.out.print(<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[0]+" "+<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[1]+" "+<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[2]+" "+<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[3]);<br> System.out.println("");<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[0] = 0f;<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[1] = 0f;<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[2] = 20f;<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[3] = 20f;<br> System.out.print(<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[0]+" "+<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[1]+" "+<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[2]+" "+<br> ARRAY_FIELDOFVIEW_DEFAULT_VALUE[3]);<br> System.out.println("");<br> }<br>}</div><div>$ java net/coderextreme/OArray<br>[-1.0, -1.0, 1.0, 1.0]<br>[0.0, 0.0, 20.0, 20.0]<br>-1.0 -1.0 1.0 1.0<br>0.0 0.0 20.0 20.0</div><div><br></div><div>So, yeah, Java socks! The only thing that isn't final/mutable is variables.</div><div><br></div><div>Don't you love the C foundation Java is based on?</div><div><br></div><div>Anyone for Haskell?</div></div><div dir="ltr"><div><br></div><div>John</div></div>
</blockquote></div></div>