Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Serializable compiler warning

  1. #1
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Serializable compiler warning

    Hi,
    I hope posted this is in the right area. My program is not dealing with File I/O but I got a warning message from the compiler indicating a serailVersionUID definition problem. Here is the warning.

    C:\J21work>javac -Xlint Exercise22_6.java
    Exercise22_6.java:6: warning: [serial] serializable class Exercise22_6 has no de
    finition of serialVersionUID
    public class Exercise22_6 extends JFrame {
    ^
    1 warning

    I'm not sure why my class is "serializable". Is it because I'm using a data structure from the JCF? I know they implement the Serializable interface. I've only read a little about that interface, it was covered briefly on a chapter about Binary I/O, but nothing was mentioned about the serialVersionUID. This is the second time I've run into this variable. The program ran fine but I was wondering what I have to do to get rid of this warning. I've listed the code below. I'd appreciate any help.

    Thanks,
    Casey



    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    import java.util.*;
     
    public class Exercise22_6 extends JFrame  {
      private LinkedList<Integer> numberList = new LinkedList<Integer>();
      private JTextField jtfNumber = new JTextField();
      private JLabel jlblEnter = new JLabel("Enter a number: ", SwingConstants.RIGHT);
      private JTextArea jtaNumbers = new JTextArea();
      private JButton jbtSort = new JButton("Sort");
      private JButton jbtShuffle = new JButton("Shuffle");
      private JButton jbtReverse = new JButton("Reverse");
     
      public Exercise22_6() {
        JPanel p1 = new JPanel(new GridLayout(1, 2));
     
    	p1.add(jlblEnter);
    	p1.add(jtfNumber);
     
    	JPanel p2 = new JPanel(new BorderLayout());
     
    	p2.add(jtaNumbers, BorderLayout.CENTER);
     
    	//JPanel p3 = new JPanel(new GridLayout(1, 3, 5, 5));
    	JPanel p3 = new JPanel();
    	p3.add(jbtSort);
    	p3.add(jbtShuffle);
    	p3.add(jbtReverse);
     
    	JPanel panel = new JPanel(new BorderLayout());
    	panel.add(p1, BorderLayout.NORTH);
    	panel.add(p2, BorderLayout.CENTER);
    	panel.add(p3, BorderLayout.SOUTH);
     
    	setLayout(new BorderLayout());
    	add(panel, BorderLayout.CENTER);
     
    	jtfNumber.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
    	    int num = Integer.parseInt(jtfNumber.getText());
    		if(!numberList.contains(num)){
    		  numberList.add(num);
    		  displayNumbers();
    		}
    		jtfNumber.setText("");
    	  }
    	});
     
    	jbtSort.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
    	    Collections.sort(numberList);
    		displayNumbers();
    	  }
    	});
     
    	jbtShuffle.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
    	    Collections.shuffle(numberList);
    		displayNumbers();
    	  }
    	});
     
        jbtReverse.addActionListener(new ActionListener() {
    	  public void actionPerformed(ActionEvent e) {
    	    Collections.reverse(numberList);
    		displayNumbers();
    	  }
    	});
     
      }
     
      public void displayNumbers() {
        String numbers = "";
    	for(Object element: numberList)
    	  numbers = numbers + element.toString() + " ";
    	jtaNumbers.setText(numbers);
     
      }
     
      public static void main(String[] args) {
        Exercise22_6 frame = new Exercise22_6();
    	frame.setSize(400, 125);
    	frame.setLocationRelativeTo(null);
    	frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    	frame.setTitle("Exercise22.6");
    	frame.setVisible(true);
     
      }
     
     
    }


  2. #2
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,307
    Thanks
    181
    Thanked 824 Times in 767 Posts
    Blog Entries
    5

    Default Re: Serializable compiler warning

    Inheritance: JFrame implements Serializable, your class extends JFrame, thus your class implements Serializable. Simply a) declare a serialVersionUID:
    static final long serialVersionUID = 1L;
    b) or use SuppressWarning annotation if you wish the compiler warning to go away,
    Last edited by copeg; September 25th, 2011 at 10:17 AM.

  3. The Following User Says Thank You to copeg For This Useful Post:

    kc120us (September 25th, 2011)

  4. #3
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Serializable compiler warning

    Quote Originally Posted by copeg View Post
    Inheritance: JFrame implements Serializable, your class extends JFrame, thus your class implements Serializable. Simply a) declare a serialVersionUID:
    static final long serialVersionUID = 1L;

    b) or use SuppressWarning annotation if you wish the compiler warning to go away,
    That worked. Thanks!! is this a common problem with Seriallizable objects?

  5. #4
    Super Moderator Sean4u's Avatar
    Join Date
    Jul 2011
    Location
    Tavistock, UK
    Posts
    637
    Thanks
    5
    Thanked 103 Times in 93 Posts

    Default Re: Serializable compiler warning

    is this a common problem
    It's not a problem, it's a feature! Read the API doc for java.io.Seralizable around the part that says:
    However, it is strongly recommended that all serializable classes explicitly declare serialVersionUID values
    You don't actually *have to* specify it, but if you're relying on default serialization for object communication / persistence, you really should know what might happen if you edit your code,

  6. #5
    Member
    Join Date
    Sep 2011
    Location
    Nanuet, NY USA
    Posts
    33
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: Serializable compiler warning

    Quote Originally Posted by Sean4u View Post
    It's not a problem, it's a feature! Read the API doc for java.io.Seralizable around the part that says:

    You don't actually *have to* specify it, but if you're relying on default serialization for object communication / persistence, you really should know what might happen if you edit your code,
    Gotcha!! Thank you.

Similar Threads

  1. suppress warning
    By mDennis10 in forum Java Theory & Questions
    Replies: 2
    Last Post: September 10th, 2011, 10:06 AM
  2. Unchecked or Unverified Warning
    By saxophonemaster in forum What's Wrong With My Code?
    Replies: 1
    Last Post: April 2nd, 2011, 08:59 PM
  3. Warning: Java component download site infected with malware
    By Darryl.Burke in forum AWT / Java Swing
    Replies: 1
    Last Post: January 27th, 2011, 03:19 AM
  4. Applet Security Warning & Class Loaders
    By tess in forum What's Wrong With My Code?
    Replies: 5
    Last Post: September 2nd, 2010, 01:41 PM
  5. what is the use of transient class and serializable interface?
    By chinni in forum Object Oriented Programming
    Replies: 3
    Last Post: October 28th, 2009, 05:48 PM