Search code examples
javaprivatepublicjgrasp

JGRASP is telling me using public or private when referencing a component or variable is wrong


 private class RoutinePanel extends JPanel
 {

  public RoutinePanel()
  {

      //labels
    private JLabel messageLabel;

      //constants for Routine Charges

    private final double OIL_CHANGE_CHARGE = 26.0;
    private final double LUBE_JOB_CHARGE = 18.0;
    private final double RADIATOR_FLUSH_CHARGE = 30.0;
    private final double TRANS_FLUSH_CHARGE = 80.0;
    private final double INSPECTION_CHARGE = 15.0;
    private final double MUFFLER_CHARGE = 100.0;
    private final double TIRE_ROTATION_CHARGE = 20.0;

JGRASP is telling me that using private in this case is an illegal operation.`My teacher specifically told us to create it that way, however. If needed I can provide the pseudo code we received from the professor.

I have also tried replacing private with public.

error message the same for all lines containing private

JoesAutomotive.java:57: error: illegal start of expression

  private JLabel messageLabel;

Solution

  • private and public are illegal inside a method definition (in this case, the constructor for RoutinePanel.)

    You need to declare class member variables in the class, outside of any method definition. Like:

    private class RoutinePanel extends JPanel
    {
    
      //labels
      private JLabel messageLabel;
    
      //constants for Routine Charges
      private final double OIL_CHANGE_CHARGE = 26.0;
      // ...
    
      public RoutinePanel()
      {