Opened 15 years ago

Last modified 15 years ago

#225 new defect

Final variables with initializer in transportable classes

Reported by: hauma Owned by: hauma
Priority: normal Milestone:
Component: uka.transport Version: 1.07h
Severity: normal Keywords:

Description (last modified by hauma)

A final variable with an initializer (like the following class A) is not allowed to be re-initialized in its constructor. Therefore, the following code is wrong:

class A {
   final int x = 13;
   A() {
      x = 42;

Unfortunately, the transportable transformation generates such code for final variables with initializers, because those variables are (re-)initialized from within the de-marshaling constructor.

Related tickets


The transportable transformation must replace all initializers of final variables with initialization code in the application constructors.

To simply ignore (not marshal) final variables with initializers is incorrect, because the initializer might produce side-effects or it could yield different results for each newly constructed object:

class B {
   final long initializationTime = System.currentTimeMillis();

Attachments (0)

Change History (2)

comment:1 Changed 15 years ago by hauma

  • Description modified (diff)

comment:2 Changed 15 years ago by hauma

In contrast to the description of the solution above, final variables with constant expressions (according to 15.28 JLS) as initializers can be excluded from marshaling, because those variables are guaranteed to be consistently re-initialized on the receiving side (given that the same version of the class is available there, but this is a prerequisite for uka.transport anyway).

In class C, the variable x has a constant expression initializer:

class C {
   final int x = 10;

Add Comment

Modify Ticket

Change Properties
Set your email in Preferences
as new The owner will remain hauma.
as The resolution will be set. Next status will be 'closed'.
to The owner will be changed from hauma to the specified user. Next status will be 'new'.
The owner will be changed from hauma to anonymous. Next status will be 'assigned'.

E-mail address and user name can be saved in the Preferences.

Note: See TracTickets for help on using tickets.