How do I declare a public class in Java?

Hello! I’m new to Replit and Java and need a little assistance getting started. I copied some sample code from my Java class (see below.)
I got an error message saying:

./Main.java:1: error: class ArrayReverseElem is public, should be declared in a file named ArrayReverseElem.java
public class ArrayReverseElem { …

I created a file named ArrayReverseElem.java, as per the message. But how do I declare a class? i.e. what do I need to put in the file?:

HERE’S THE CODE:

public class ArrayReverseElem {
   public static void main(String[] args) {
      Scanner scnr = new Scanner(System.in);
      final int NUM_ELEMENTS = 8;             // Number of elements
      int[] userVals = new int[NUM_ELEMENTS]; // User numbers
      int i;                                  // Loop index
      int tempVal;                            // Temp variable for swapping

      // Prompt user to input values
      System.out.println("Enter " + NUM_ELEMENTS
              + " integer values...");
      for (i = 0; i < userVals.length; ++i) {
         System.out.print("Value: ");
         userVals[i] = scnr.nextInt();
      }

      // Reverse array's elements
      for (i = 0; i < userVals.length; ++i) {
         tempVal = userVals[i];                        // Temp for swap
         userVals[i] = userVals[userVals.length - 1 - i]; // First part of swap
         userVals[userVals.length - 1 - i] = tempVal;     // Swap complete
      }

      // Print numbers
      System.out.print("\nNew values: ");
      for (i = 0; i < userVals.length; ++i) {
         System.out.print(userVals[i] + " ");
      }
   }
}

Good references:
Java Classes and Objects (w3schools.com)
Types of Classes in Java - GeeksforGeeks

1 Like