Click here to Skip to main content
15,903,385 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Main.java:9: error: Illegal static declaration in inner class Main.SortAsc
			public static void main(String[] args) {         
			                   ^
  modifier 'static' is only allowed in constant variable declarations



can anyone help with this error

What I have tried:

import java.io.*; // for handling input/output
import java.util.*; // contains Collections framework

// don't change the name of this class
// you can add inner classes if needed
class Main {
	public class SortAsc {      
			public static void main(String[] args) {         
        int [] arr = new int [] {5, 2, 8, 7, 1};   
        int temp = 0;  
        System.out.println("Elements of original array: ");  
        for (int i = 0; i < arr.length; i++) {   
            System.out.print(arr[i] + " ");  
        }  
        for (int i = 0; i < arr.length; i++) {   
            for (int j = i+1; j < arr.length; j++) {   
               if(arr[i] > arr[j]) {  
                   temp = arr[i];  
                   arr[i] = arr[j];  
                   arr[j] = temp;  
               }   
            }   
        }  
        System.out.println();  
        System.out.println("Elements of array sorted in ascending order: ");  
        for (int i = 0; i < arr.length; i++) {   
            System.out.print(arr[i] + " ");  
        }  
    }  
}  
}
Posted
Updated 2-Oct-21 7:00am

1 solution

You should only have one class statement. There is no need for an inner class in this code. And it is pointless adding one that does nothing.
Java
// you can add inner classes if needed
// remove this line -->          class Main {
	public class SortAsc {      
			public static void main(String[] args) {         
        int [] arr = new int [] {5, 2, 8, 7, 1};   
        int temp = 0;  
        System.out.println("Elements of original array: ");  
        for (int i = 0; i < arr.length; i++) {   
            System.out.print(arr[i] + " ");  
        }  
        for (int i = 0; i < arr.length; i++) {   
            for (int j = i+1; j < arr.length; j++) {   
               if(arr[i] > arr[j]) {  
                   temp = arr[i];  
                   arr[i] = arr[j];  
                   arr[j] = temp;  
               }   
            }   
        }  
        System.out.println();  
        System.out.println("Elements of array sorted in ascending order: ");  
        for (int i = 0; i < arr.length; i++) {   
            System.out.print(arr[i] + " ");  
        }  
    }  
}  
// and remove this }

Remove the two lines indicated, and save the file as SortAsc.java
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900