[x3d-public] Better approachs, was: Re: Java SOCKS! jsail/Navigation/OrthoViewpoint.java in X3DJSAIL

Don Brutzman don.brutzman at gmail.com
Wed Jul 30 13:21:17 PDT 2025


John, when trying to replicate the situation reported in your email, I
modified HelloWorldProgram.java on line 220 to insert one line.

   - OrthoViewpoint.FIELDOFVIEW_DEFAULT_VALUE =
   ArrayList<MFFloat>(Arrays.asList(-10f,-10f,10f,10f)); // illegal

Since OrthoViewpoint.FIELDOFVIEW_DEFAULT_VALUE is *static *(a class
variable) and *final* (immutable, unchangeable) such a reassignment
statement is expected to fail Java compilation checks.

For me, attempting to override this immutable property indeed fails.

Java compiler reports:

   - [parsing started SimpleFileObject[C:\x3d-code\www.web3d.org
   \x3d\stylesheets\java\examples\HelloWorldProgram.java]]
   C:\x3d-code\www.web3d.org\x3d\stylesheets\java\examples\HelloWorldProgram.java:220:
   error: illegal start of expression
           OrthoViewpoint.FIELDOFVIEW_DEFAULT_VALUE =
   ArrayList<>(Arrays.asList(-10f,-10f,10f,10f));

NetBeans IDE directly shows failing line and provides tooltip on the
offending source

   - [image: image.png]
   - with tooltip "Cannot assign a value to static final variable
   FIELDOFVIEW_DEFAULT_VALUE"

So I am not seeing any problem to fix in X3DJSAIL, the library is indeed
protecting programmers from making unintended errors, exactly as designed.

Have fun with strictly structured X3D Java using X3DJSAIL!

all the best, Don

On Tue, Jul 29, 2025 at 11:36 PM John Carlson <yottzumm at gmail.com> wrote:

> Corrections, learn something new daily!
>
> 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.
>
> Also, the AI says:
>
> import java.util.Arrays;
> import java.util.Collections;
> import java.util.List;
>
> /* [ 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()???*/
>
> public class MyClass {
> private final List<Integer> immutableList;
>
> public MyClass(int[] initialArray) {
> this.immutableList =
> Collections.unmodifiableList(Arrays.asList(Arrays.stream(initialArray).boxed().toArray(Integer[]::new)));
> }
>
> public List<Integer> getImmutableList() {
> return immutableList;
> }
> }
>
> Reference to documentation:
>
> https://docs.oracle.com/javase/8/docs/api/java/util/Collections.html#unmodifiableList-java.util.List-
>
> There are other unmodifiable collections.
>
> I’ll probably mess with my OArray.java to try this out, interesting!
>
> On Tue, Jul 29, 2025 at 11:55 PM John Carlson <yottzumm at gmail.com> wrote:
>
>> 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.
>>
>> The magenta?  Looks wrong.  A final variable got overwritten.
>> ##########################################
>> ((((getFieldOfView().length > 0) &&
>> !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)) ||
>> !ConfigurationProperties.getStripDefaultAttributes()) /* ArrayList .x3d
>> compare */ && !hasUSE())=false
>> getFieldOfView().length > 0=true
>> !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)=false
>> !ConfigurationProperties.getStripDefaultAttributes()=false
>> !hasUSE()=true
>> getFieldOfViewList()=[0.0, 0.0, 20.0, 20.0]
>> FIELDOFVIEW_DEFAULT_VALUE=[0.0, 0.0, 20.0, 20.0]
>> ##########################################
>>
>> Later, the magenta is correct.  Why?
>>
>> ##########################################
>> ((((getFieldOfView().length > 0) &&
>> !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)) ||
>> !ConfigurationProperties.getStripDefaultAttributes()) /* ArrayList .x3d
>> compare */ && !hasUSE())=false
>> getFieldOfView().length > 0=true
>> !getFieldOfViewList().equals(FIELDOFVIEW_DEFAULT_VALUE)=false
>> !ConfigurationProperties.getStripDefaultAttributes()=false
>> !hasUSE()=true
>> getFieldOfViewList()=[-1.0, -1.0, 1.0, 1.0]
>> FIELDOFVIEW_DEFAULT_VALUE=[-1.0, -1.0, 1.0, 1.0]
>> ##########################################
>>
>> Here's the definition of the variable:
>>
>> /** 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). */
>> public static final ArrayList<Float> FIELDOFVIEW_DEFAULT_VALUE = new
>> ArrayList<>(Arrays.asList(-1f,-1f,1f,1f));
>>
>> See example code to test (it gets even worse, even arrays themselves are
>> mutable.
>>
>> import java.util.ArrayList;
>> import java.util.Arrays;
>>
>> public class OArray {
>>         public static final ArrayList<Float> FIELDOFVIEW_DEFAULT_VALUE =
>> new ArrayList<>(Arrays.asList(-1f,-1f,1f,1f));
>>         public static final float[] ARRAY_FIELDOFVIEW_DEFAULT_VALUE = new
>> float [] {-1f,-1f,1f,1f};
>>         public static void main(String[] args) {
>>                 System.out.println(FIELDOFVIEW_DEFAULT_VALUE);
>>                 FIELDOFVIEW_DEFAULT_VALUE.set(0, 0f);
>>                 FIELDOFVIEW_DEFAULT_VALUE.set(1, 0f);
>>                 FIELDOFVIEW_DEFAULT_VALUE.set(2, 20f);
>>                 FIELDOFVIEW_DEFAULT_VALUE.set(3, 20f);
>>                 System.out.println(FIELDOFVIEW_DEFAULT_VALUE);
>>                 System.out.print(
>>                                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[0]+" "+
>>                                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[1]+" "+
>>                                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[2]+" "+
>>                                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[3]);
>>                 System.out.println("");
>>                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[0] = 0f;
>>                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[1] = 0f;
>>                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[2] = 20f;
>>                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[3] = 20f;
>>                 System.out.print(
>>                                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[0]+" "+
>>                                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[1]+" "+
>>                                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[2]+" "+
>>                                 ARRAY_FIELDOFVIEW_DEFAULT_VALUE[3]);
>>                 System.out.println("");
>>         }
>> }
>> $ java net/coderextreme/OArray
>> [-1.0, -1.0, 1.0, 1.0]
>> [0.0, 0.0, 20.0, 20.0]
>> -1.0 -1.0 1.0 1.0
>> 0.0 0.0 20.0 20.0
>>
>> So, yeah, Java socks!  The only thing that isn't final/mutable is
>> variables.
>>
>> Don't you love the C foundation Java is based on?
>>
>> Anyone for Haskell?
>>
>> John
>>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20250730/0e9f3217/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: image.png
Type: image/png
Size: 23904 bytes
Desc: not available
URL: <http://web3d.org/pipermail/x3d-public_web3d.org/attachments/20250730/0e9f3217/attachment-0001.png>


More information about the x3d-public mailing list