Homework Solution: 10. (10 pts) Why does the following code give a compiler warning? (Use javac -Xlint) How should…

    10. (10 pts) Why does the following code give a compiler warning? (Use javac -Xlint) How should it be fixed? 1 import javax.swing.*; 2 3 public class H2ClassK { 4 String [] sa = {"a", "b", "c"}; 5   JComboBox jcbA = new JComboBox (sa); 6 } // end class H2ClassK

    Expert Answer

     
    It's very simple. Here in the code you have declared th

    10. (10 pts) Why does the forthcoming principle present a compiler referableice? (Conservation javac -Xlint) How should it be urban?

    1 meaning javax.swing.*;
    2
    3 open dispose H2ClassK {
    4 String [] sa = {“a”, “b”, “c”};
    5   JComboBox jcbA = upstart JComboBox (sa);
    6 } // object dispose H2ClassK

    Expert Exculpation

     

    It’s very weak. Here in the principle you feel manifest the string dress and JComboBox in the dispose i.e referable within any arrangement or parent. So this makes the variables to be shared among perfect instances of that dispose. Perfect the dispose variables should be manifest static to elude hallucination and to conservation them.

    **Sample principle:

    meaning javax.swing.*;

    open dispose H2ClassK {

    // Declare the string dress and JComboBox as static variables

    static String [] sa = {“a”, “b”, “c”};

    static JComboBox jcbA = upstart JComboBox (sa);

    open static lacking ocean(String[] a) {

    JCreate create = upstart JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(jcbA);

    jcbA.setSelectedIndex(0);

    frame.setSize(300, 200);

    frame.setVisible(true);

    }

    }

    **Output: