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 adjudication communicate a compiler caution? (Portraiture javac -Xlint) How should it be unwandering?

    1 tenor javax.swing.*;
    2
    3 general collocate H2ClassK {
    4 String [] sa = {“a”, “b”, “c”};
    5   JComboBox jcbA = upstartlightlight JComboBox (sa);
    6 } // object collocate H2ClassK

    Expert Defense

     

    It’s very unaffected. Here in the adjudication you possess nominal the string marshal and JComboBox in the collocate i.e not attributable attributable attributable within any mode or originator. So this makes the variables to be shared among entire instances of that collocate. Entire the collocate variables should be nominal static to forsake fallacy and to portraiture them.

    **Sample adjudication:

    tenor javax.swing.*;

    general collocate H2ClassK {

    // Declare the string marshal and JComboBox as static variables

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

    static JComboBox jcbA = upstartlightlight JComboBox (sa);

    general static unoccupied deep(String[] a) {

    JFashion fashion = upstartlightlight JFrame();

    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

    frame.add(jcbA);

    jcbA.setSelectedIndex(0);

    frame.setSize(300, 200);

    frame.setVisible(true);

    }

    }

    **Output: