Serializable class doesn�t declare static final serialVersion of type long

The serializable class SomeClassName does not declare a static final serialVersionUID field of type long
I have a lot of these warnings in my current application.
What does it really mean?
In what situations do I actually want/need to use this?
If I need it somehwere, do I just declare this field?

Putting a version number on a serializable class is a precaution against the class definition changing in a way that confuses ObjectStreams when reading old serialisations.
You can just declare the varialble, initially make it 1 and crank it up in your source when you change the fields of the class.

Similar Messages

  • ERROR: serializable class HelloComponent does not declare a static final

    Hi everyone! I'm sorry but I'm a newbie to Java and I'm having some, I assume, basic problems learning to compile and run with javac. Here is my code:
    import javax.swing.* ;
    import java.awt.* ;
    public class MyJava {
    * @param args
    public static void main(String[] args)
    // TODO Auto-generated method stub
    JFrame frame = new JFrame( "HelloJava" ) ;
    HelloComponent hello = new HelloComponent() ;
    frame.add( hello ) ;
    frame.setSize( 300, 300 ) ;
    frame.setVisible( true ) ;
    class HelloComponent extends JComponent
    public void paintComponent( Graphics g )
    g.drawString( "Hello Java it's me!", 125, 95 ) ;
    And here is the error:
    1. WARNING in HelloJava.java
    (at line 20)
    class HelloComponent extends JComponent {
    ^^^^^^^^^^^^^^
    The serializable class HelloComponent does not declare a static final serialVersionUID field of type long
    ANY HELP WOULD BE GREAT! THANKS! =)

    Every time I extend GameLoop, it gives me the warning
    serializable class X does not declare a static final serialVersionUID field of type long
    This is just a warning. You do not need to fix this because the class you are writing will never be serialized by the gaming engine. If you really want the warning to go away, add the code to your class:
    static final long serialVersionUID=0;

  • The serializable class SpacePainter does not declare a static final serial

    The serializable class SpacePainter does not declare a static final serialVersionUID field of type longWhat does this mean??? It appears as a warning in Eclipse but I have no idea what it is. It happens when I create a class the extends JFrame or JCompnent or JApplet. I finnally got it to stop with this:
    static final long serialVersionUID = 1;

    Because your eclipse is configured that way. You can probably filter the warning. You don't have to implement the serialVersionUID, but you should if you really serialize the exceptions.

  • Static final in (Class or Interface)

    Hello Friends ,
    What is the difference in having a public static final variable in an class and having the same kind of
    declarations in an Interface ?
    Does the interface is better or having it in a class ?
    Thanks in Advance,
    -S

    shyamnambiar wrote:
    But I'm not clear . you mean to say its all the same .It does not make any difference ?The only difference that it makes (and that might be an important difference) is what you communicate with your design an intention.
    Constants should usually be placed within their context since that's where they are going to be used, and that's where developers will look. It's more common to place the constants in the class that they are related to. They should not be placed in an empty final class just because you want to group constants. You should also avoid placing constants in an interface just because you then can implement that interface to "get hold of" the constants.
    Kaj

  • Declaring public static final variables in jsp?

    The question is in the title...
    I know this is not a jsp forum, but some people here might know if it's possible or not.
    If yes, how to declare them and how to access them from other jsp pages?
    Thx

    If you need that, you definitely do too much work in your JSPs. You should do all your work in Servlets (or Actions if you're using Struts or something similar).
    Your JSPs should only do the presentation.
    Remember that JSPs are not classes (they are compiled into classes internally, but you don't have direct access to those).
    If you absolutely need such a public static final field that you want to access from several JSPs, just define it in a Class and reference that class from your JSPs.

  • Will declaring primitives as static final (constants) conserve memory?

    I wrote a code
    int a = flag?1:0;on which my supervisor commented that 1 and 0 should be replaced by SomeInterface.ONE (=1) and SomeInterface.ZERO (=0) respectively. The reason he gave was that since constants are static so it will conserve memory.
    Is this really true?

    First of all, this Interface for Constants anti-pattern is widely derided.
    Secondly, one of the reasons it was used in the first place was readability, not performance, and in that case constants that pertain to the meaning of that 1 and 0, and not the values, might have been understandable (e.g. SomeInterface.TRUE = 1 and SomeInterface.FALSE = 0), but definately not ONE and ZERO.
    Thirdly, memory use is the same as public static final variables (as interface constants are), have their value compiled directly into the class using them, at the point where it used, so whether a constant is used, or a literal value, the space that that class definition uses is the same. In addition, there is then an interface definition that also has those values so the memory use is more, in end effect. And, regardless, that memory space is not on the heap, which is the memory space you will be most interested in, so it has no noticeable effect on memory space either way.

  • Why String class was declared as final.

    Why String class was declared as final.
    Thanks
    Mohan

    http://bugs.sun.com/bugdatabase/view_bug.do;jsessionid=d92720bd3ed4dffffffff95e59403ecf4db1:YfiG?bug_id=4095367

  • Using java class and variables declared in java file in jsp

    hi everyone
    i m trying to seperate business logic form web layer. i don't know i am doing in a right way or not.
    i wanted to access my own java class and its variables in jsp.
    for this i created java file like this
    package ris;
    import java.sql.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class  NewClass{
        public static void main(String args[]){
            Connection con = null;
            ResultSet rs=null;
            Statement smt=null;
            try{
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con=DriverManager.getConnection("jdbc:mysql:///net","root", "anthony111");
                smt=con.createStatement();
               rs= smt.executeQuery("SELECT * FROM emp");
               while(rs.next()){
                String  str = rs.getString("Name");
                }catch( Exception e){
                    String msg="Exception:"+e.getMessage();
                }finally {
          try {
            if(con != null)
              con.close();
          } catch(SQLException e) {}
    }next i created a jsp where i want to access String str defined in java class above.
    <%--
        Document   : fisrt
        Created on : Jul 25, 2009, 3:00:38 PM
        Author     : REiSHI
    --%>
    <%@page import="ris.NewClass"%>
    <%@page contentType="text/html" pageEncoding="UTF-8"%>
    <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
       "http://www.w3.org/TR/html4/loose.dtd">
    <html>
        <head>
            <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
            <title>JSP Page</title>
        </head>
        <body>
            <h1><%=str%></h1>
        </body>
    </html>I wanted to print the name field extracted from database by ResultSet.
    but it gives error cannot find symbol str.
    please help me to find right way to do this.
    i am using netbeans ide.

    Very bad approach
    1) Think if your table contains more than one NAMEs then you will get only the last one with your code.
    2) Your String is declared as local variable in the method.
    3) You have not created any object of NewClass nor called the method in JSP page. Then who will call the method to run sql?
    4) Your NewClass contains main method which will not work in web application, it's not standalone desktop application so remove main.
    Better create an ArrayList and then call the method of NewClass and then store the data into ArrayList and return the ArrayList.
    It should look like
    {code:java}
    package ris;
    import java.sql.*;
    import java.sql.Connection;
    import java.sql.DriverManager;
    import java.sql.SQLException;
    public class  NewClass{
        public static ArrayList getNames(){
            Connection con = null;
            ResultSet rs=null;
            Statement smt=null;
            ArrayList nameList = new ArrayList();
            try{
                Class.forName("com.mysql.jdbc.Driver").newInstance();
                con=DriverManager.getConnection("jdbc:mysql:///net","root", "anthony111");
                smt=con.createStatement();
               rs= smt.executeQuery("SELECT * FROM emp");
               while(rs.next()){
                nameList.add(rs.getString("Name"));
               return nameList;
                }catch( Exception e){
                    String msg="Exception:"+e.getMessage();
                   </code><code class="jive-code jive-java"><font>return nameList;</code><code class="jive-code jive-java">
                }finally {
          try {
            if(con != null)
              con.close();
          } catch(SQLException e) {}
          </code><code>return nameList;</code>
    <code class="jive-code jive-java">    }

  • XSQL-022 error on using FOP - serializer class not found; urgent!

    Hi all,
    currently using XDK 9.2.0.1.0. On trying to invoke Apache FOP serializer I just get the following error message:
    XSQL-022: Cannot load serializer class oracle.xml.xsql.serializers.fopserializer
    and I really don't know why....
    Running XSQL Servlet within Tomcat 4 Engine, but error pops up using both, Tomcat way and/or just the commandline invoker xsql.bat.
    Generally XSQL Servlet is working correctly.
    My classpath setting contains:
    xmlparserv2.jar; batik.jar; classes12.jar; fop.jar; fopserializer.jar; oraclexsql.jar; sax2.jar; xalan-2.0.0.jar; xalanj1compat.jar; xerces-1.2.3.jar; xschema.jar; XSQLConfig.jar; xsu12.jar
    The serializer being redefined in XSQLConfig is just:
    <serializer>
    <name>FOP203</name>
    <class>oracle.xml.xsql.serializers.fopserializer</class>
    </serializer>
    This is because of using FOP 0.20.3, so having my own serializer class due to API change in FOP 0.20.x; my fopserializer is looking like that:
    package oracle.xml.xsql.serializers;
    import org.w3c.dom.Document;
    import java.io.PrintWriter;
    import oracle.xml.xsql.*;
    import org.apache.fop.messaging.MessageHandler;
    import org.apache.fop.apps.*;
    import java.io.*;
    // FOP 0.20.3 Serializer implementation for XSQL
    public class fopserializer implements XSQLDocumentSerializer
    private static final String PDFMIME="application/pdf";
    public void serialize(Document doc, XSQLPageRequest req) throws Throwable
    try
    Driver FOPDriver = new Driver();
    FOPDriver.setRenderer(FOPDriver.RENDER_PDF);
    MessageHandler.setOutputMethod(MessageHandler.NONE);
    FOPDriver.setupDefaultMappings();
    req.setContentType(PDFMIME);
    FOPDriver.setOutputStream(req.getOutputStream());
    FOPDriver.render(doc);
    catch (Exception e)
    e.printStackTrace(System.err);
    In my XSQL file I'm using the following PI:
    <?xml-stylesheet type="text/xsl" href="listemps.fo" serializer="FOP203"?>
    Any suggestions what is wrong in here ?
    Can't use the original xsqlserializer.jar either - same error.
    Thanks in advance
    Jochen

    Hi Jochen,
    I'm working with Tomcat 4.0.3, FOP 20.0.3 and XDK 9.2.0.1.0, too.
    I've copied all the jar-files into tomcat/common/lib. I think that you can't use the MessageHandler in fop20.0.3. I'm using the following serializer:
    package diva.xml.xsql.serializers;
    import org.w3c.dom.Document;
    import java.io.PrintWriter;
    import oracle.xml.xsql.*;
    import org.apache.fop.apps.*;
    import org.apache.log.*;
    import org.apache.log.format.*;
    import org.apache.log.output.io.*;
    import org.apache.avalon.*;
    import java.io.*;
    public class XSQLFOP203Serializer implements XSQLDocumentSerializer {
    private static final String PDFMIME = "application/pdf";
    private static final String CONFPATH = "/usr/local/fop/conf/userconfig.xml";
    public void serialize(Document doc, XSQLPageRequest env) throws Throwable {
    try {
    // Open user config file
    File userConfigFile = new File(CONFPATH);
    Options options = new Options(userConfigFile);
    // First make sure we can load the driver
    Driver FOPDriver = new Driver();
    // Setup logging
    Hierarchy hierarchy = Hierarchy.getDefaultHierarchy();
    PatternFormatter formatter = new PatternFormatter("[%{priority}]: %{message}\n%{throwable}" );
    LogTarget target = null;
    target = new StreamTarget(System.out, formatter);
    hierarchy.setDefaultLogTarget(target);
    Logger log = hierarchy.getLoggerFor("fop");
    log.setPriority(Priority.INFO);
    FOPDriver.setLogger(log);
    // Then set the content type before getting the reader/
    env.setContentType(PDFMIME);
    FOPDriver.setRenderer(FOPDriver.RENDER_PDF);
    FOPDriver.setOutputStream(env.getOutputStream());
    FOPDriver.render(doc);
    catch (Exception e) {
    // Cannot write PDF output for the error anyway.
    // So maybe this stack trace will be useful info
    e.printStackTrace(System.err);
    Hope this helps
    Uwe

  • NullPointerException (Unknown Source) in Serializable class

    Hi, I have a Serializable class, and I just added a new class variable to it, with its get and set methods. When I try to call either the get or the set method from outside the class, I get a NullPointerException, which refers to the method in the following format:
    java.lang.NullPointerException
    at setMethod (Unknown Source)
    or at getMethod (Unknown Source)I'm very confused about why this is happening, and I can't seem to come up with any solutions as to why it happens only for this variable and not for the other ones.
    I wrote the two following files, trying to reproduce this problem outside work, but I wasn't successful. Please help out!
    import java.io.Serializable;
    public class Profile implements Serializable {
         private static final long serialVersionUID = 1835312781905234523L;
        private String name;
        private String phone;
        private String city;
        private int age;
        public Profile() {}
        public Profile(String name, String phone, String city, int age)
             this.name = name;
             this.phone = phone;
             this.city = city;
             this.age = age;
        public String getName()
             return name;
        public void setName(String name)
             this.name = name;
        public String getPhone()
             return phone;
        public void setPhone(String phone)
             this.phone = phone;
        public String getCity()
             return city;
        public void setCity(String city)
             this.city = city;
        public int getAge()
             return age;
        public void setAge(int age)
             this.age = age;
    public class ProfileTester {
        Profile profile;
         public ProfileTester()
             profile = new Profile();
             profile.setName("Nazli");
             profile.setPhone("650-234-1234");
             profile.setCity("Menlo Park, CA");
             profile.setAge(30);
             System.out.println("Name: "+profile.getName());
             System.out.println("Phone: "+profile.getPhone());
             System.out.println("City: "+profile.getCity());
             System.out.println("Age: "+profile.getAge());
         public static void main (String [] args)
              ProfileTester tester = new ProfileTester();
    }

    My guess is that your get or set method is a little more complex than a simple retrieval or assignment.
    Make sure there are no null references in the body of those methods.

  • Instantiation of static final objects

    Is there a problem with coding the following?:
    class A {
    public static final A static_final_a_obj = new A ( B.static_final_b_obj );
    private B b;
    public A ( B b ) {
    this.b = b;
    class B {
    public static final B static_final_b_obj = new B ( A.static_final_a_obj );
    private A a;
    public B ( A a ) {
    this.a = a;
    I put log messages in each of the constructors to see if it would cause an infinite loop, but it doesn't seem to. The question is 'why'.. :) How can either of the static final objects ever be fully instantiated?

    Exactly. It is not executed infinitely.
    class A {
    public static final A static_final_a_obj = new A ( B.static_final_b_obj );
    private B b;
    public A ( B b ) {
    System.out.println(" A Cons");
    this.b = b;
    public static void main(String args[]) {
         System.out.println("A");
    Output:
    B Cons
    A Cons
    A
    class B {
    public static final B static_final_b_obj = new B ( A.static_final_a_obj );
    private A a;
    public B ( A a ) {
    System.out.println(" B Cons");
    this.a = a;
    public static void main(String args[]) {
         System.out.println("B");
    Output:
    A Cons
    B Cons
    B
    Edited by: prajeshps on Sep 18, 2007 1:51 PM

  • Why a Constant has to be of type Static final  rather than final?

    Hi,
    I struck up with a basic doubt
    Why a Constant has to be of type Static final rather than final?
    Hoping for quick reply
    Venu.

    Hi,
    I struck up with a basic doubt
    Why a Constant has to be of type Static final rather
    than final?It doesn't have to be, but it usually makes more sense for it to be static (associated with the class) than non-static (associated with the instance). If every instance of the class will have the same value for that variable--whether it's final and a constant or not--it makes sense to make it static. There's no need for multiple copies of the same value. (And in the case of non-final variables, it can lead to incorrect program behavior if it's not static.) If each instance of the class could potentially have a different value for that variable, then it has to be non-static.

  • Where can I declare a final variable?

    I'm wondering where excatly I can declare a final variable. I know I can declare them right after I start the class.
    i.e.:
    public class x {
         public static final Scanner CONSOLE = new Scanner(System.in);
         public static final Random GENERATOR = new Random();
    }I would like to declare a final within the main method. i.e.:
         public static void main(String[] args) {
              System.out.print("Please type in your name and press return: ");
              final String NAME = CONSOLE.next();
         }Is this just not possible or am I just writing it wrong? Couldn't find an example using google. Any help?
    fyi: I'm fairly new to using java so this may be obvious.

         public static void main(String[] args) {
    System.out.print("Please type in your name and
    nd press return: ");
              final String NAME = CONSOLE.next();
         }Is this just not possible or am I just writing it
    wrong?No, you can declare a local variable final, which means that it's value can only be set once in or after the declaration. But be aware that a new variable is created each time the method is invoked.
    About the only reason people do this is so it can be used in a local class declared further down the method.

  • Client can't access Serializable classes on Server

    Hi there, i have created a very simple client server model and had it running fine. I then added a new serializable class to the server and return it in a new method. Now when i try to run that method on the client side it complains it cannot find my new serializable class ("no class def found exception: rmi/server/ServerObject.class"). This is how it's setup at the moment:
    Server
    -ServerFront extends Remote
    -Server implements ServerFront
    -ServerObject implements Serializable
    The server binds itself to the registry as such:
    ServerFront engine = new Server();
    String name = "Server";
    ServerFront stub = (ServerFront)UnicastRemoteObject.exportObject(engine, 0);
    Registry registry = LocateRegistry.getRegistry();
    registry.rebind(name, stub);The ServerObject class looks like this:
    public class ServerObject implements Serializable
        private static final long serialVersionUID = 10L;
        //methods
    }The ServerFront interface has a method as such:
    ServerObject getServerObject () throws RemoteException;    Client
    -Client
    The client gets a reference to the server as follows:
    String name = "Server";
    Registry registry = LocateRegistry.getRegistry("127.0.0.1");
    server = (ServerFront)registry.lookup(name);Then tries to run the getServerObject() method:
    ServerObject = server.getServerObject();I run the client and server from jar files using the following commands:
    start java -Djava.rmi.server.codebase="http://www.xenwars.com/Server.jar" -Djava.security.policy=security.policy -jar Server.jar
    java -Djava.security.policy=security.policy -jar Client.jar"security.policy" allows all access, and the codebase URL points to my webserver where i uploaded an identical copy of the Server jar file that i'm trying to run.
    So... i've read all the Sun documentation and such and i'm massively confused why my client isn't simply downloading the serializable class as it's supposed to. Any help is massively appreciated, thanks :D
    Edited by: Aldarn on Feb 8, 2009 10:03 AM
    Edited by: Aldarn on Feb 8, 2009 10:05 AM

    By a mirrored interface i mean i just created an interface based off my existing serializable classes, i.e. copied the method constructors into an interface class and made the serializable class implement that interface.
    I think that must have been the problem then, that i wasn't giving the client an interface to the serializable classes on the server that it was attempting to download. I thought by using RMI on a serializable class it would just download that class when it needs it and so wouldn't need an interface pre-defining it.
    Thanks for the help! I'm now working on a system with 3 servers and one client, where two servers have various methods implemented and the third one acts as a bridge between the other two servers and the client - so the client only has to deal with one server. It's working fine but it's pretty annoying having to mirror all the methods from both servers' interface classes in the server that acts as a bridge's interface class - is there any way that the bridge server can inherit the other two servers interfaces into its own so i don't need to make loads of duplicate "shell" methods like this:
    // second server
    public ArrayList<Thing> getAllThings ()
        return things;
    // bridge server
    private SecondServerFront secondServer; // reference loaded in constructor
    public ArrayList<Thing> getAllThings () throws RemoteException
        return secondServer.getAllThings();
    }  

  • Static final

    I need to share several objects among all instances, so I mark them as static. As far as these variables should be initialized only once they must be final as well. The problem is that I can't use static initializer, because these final static objects are constructed by 2nd class.
    class Y {
         private static final var1;
         private static final var2;
         Y() {}
         static void useVars() { ... }
         void useVars() { ... }
    class Main {
         public static void main(String[] args) {
              v1 = getVar1();
              v2 = getVar2();
              initClassY(v1, v2); // initializing static final vars for Y
    }

    You can use blank finals. This allows one to declare
    a final without initializing it.This is correct.
    The catch is you must initialize it before you use it.This is true, but not exact.
    Actually, you must initialize it in the constructor
    The following will not compile :
    public MyClass{
       final int BLANK_FINAL;
       public MyClass(int arg1){
          setBF(arg1);
       private void setBF(int arg1){
          BLANK_FINAL = arg1 * 200 + 86; //compiler error

Maybe you are looking for

  • How can I schedule an export to excel format of an OBIEE Analysis or Dashboard to a local directory ?

    Hello, I would like to schedule with an agent some reports in OBIEE 11g that must be exported in excel format to a specific local directory. There is a need for a group of OBIEE users to have a set of reports ready (exported in .xlsx) every morning.

  • Software for mac

    when are nokia going to give us mac users some decent software rather than their **bleep**py syncing software?? i have an n95 but have no way to install anything to it like the maps unless i find someone with a windows pc. i think in this day and age

  • ISP and Apple can't help me...

    I'm getting frustrated with my iPod Touch. I can't connect on Internet and set up mail. I have a wireless modem and a router (Linksys WirelessG 2.4) -- the signal comes through at full strength on the Touch, but nothing shows up in the router or DNS

  • CP: I v1.3 Upgrade Switch & Router IOS?

    From everything I've read, it does not look like Prime: Infrastructure is capable of upgrading the IOS of Catalyst Switches and IOS Routers. I just wanted to give a shout out incase I am wrong.            Does anyone know if this feature will be avai

  • [JS] [CS4] Right to left text when Placing

    Hey. In CS4 - If I want text to flow right-to-left for an entire story when doing a text file 'Place' what attributes do I need to set? Thanks for your help.