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
Code :
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);
}
}
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,
Re: Serializable compiler warning
Quote:
Originally Posted by
copeg
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?
Re: Serializable compiler warning
Quote:
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:
Quote:
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,
Re: Serializable compiler warning
Quote:
Originally Posted by
Sean4u
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.