Note that there are some explanatory texts on larger screens.

plurals
  1. POstatics inside inner classes : serialVersionUID
    primarykey
    data
    text
    <p>I'm trying to develop my personal library classes but have been running into a roadblock. My utilities class consists of static methods and constants, and several public inner classes for various data structures and subutilities. These classes are NOT static nested classes, they are intended to be instanced, hold data, etc.</p> <p>The problem is like any data class I want to make it serializable. That in turn means I want to set the serialVersionUID (even though I use my own versioning methods), and that of course cant be done because you cannot have a static variable in an inner class. </p> <p>Is there a way around this? Does the parent class have to hold the UID (even though it's effectively static). At the moment I'm starting to consider turning my Utilities class into a set of classes, and giving up on the idea of a single import utility class. It is turning into an increasingly big set of utilities though.</p> <p>K.Barad JDK1.6 u23</p> <p>edit: quick example code. </p> <pre><code>public class UtilDummy { public static final Date version = new Date(111,01,04); public static final int SUCCESS = 0; public static final int READ_FAIL = -1; public class TupleComp&lt;E1 extends Serializable, E2 extends Serializable, E3 extends Serializable&gt; implements Cloneable, Serializable { /** version : Date object for the version of this class */ public final Date version = new Date(111, 01, 04); public E1 elem1 = null; public E2 elem2 = null; public E3 elem3 = null; public TupleComp() { } public TupleComp(E1 elem1, E2 elem2, E3 elem3) { this.elem1 = elem1; this.elem2 = elem2; this.elem3 = elem3; } @Override public TupleComp&lt;E1, E2, E3&gt; clone() { return new TupleComp&lt;E1, E2, E3&gt;(elem1, elem2, elem3); } public boolean equals(TupleComp&lt;E1, E2, E3&gt; target) { boolean out = true; out &amp;= (elem1 == null ? target.elem1 == null : elem1.equals(target.elem1)); out &amp;= (elem2 == null ? target.elem2 == null : elem1.equals(target.elem2)); out &amp;= (elem3 == null ? target.elem3 == null : elem1.equals(target.elem3)); return out; } } } </code></pre> <p>the inner class throws up warnings, and a fairly relevant one, but since it cannot take static fields I cannot add public static final long serialVersionUID</p> <p><strong>edit</strong>: Ok, so a line like public static final long ll = 1; is fine. The matter is what I really want to do is have: public static final Date version = new Date(111 , 01 , 04 ); public static final long serialVersionUID = version.getTime();</p> <p>This works in a top level class, but in an inner classes comes up with: "The field version cannot be declared static; static fields can only be declared in static or top level types"</p> <p>Dates are for me the most practical way of storing a meaningful and incremental version tracking system which is comparable and can be split into major and minor parts. Does anyone know what about the Date object is causing the trouble;</p> <p><strong>Closed</strong>:</p> <p>It seems there was some misunderstanding on my part on how a static class behaves, and that this is the solution to the problem. I'll probably (eventually) split the classes, but for now this is the most sensible solution. Thanks all who answered</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload