I want to know whether it is fine to put a class inside a Main class as the code snippet shown below? This code works fine but just want to make sure whether this is correct or not,
putting a class in static way inside the main class without making a making whole separate class.
For example;
public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); LinearSearch ls = new LinearSearch(); int[] arr = {20, 46, 22, 19, 6, 42, 14, 5, 48, 47, 17, 39, 51, 7, 2}; System.out.println("Element to be found: "); int x = sc.nextInt(); ls.linearSearch(arr, x); } public static class LinearSearch { int linearSearch(int[] arr, int target) { for(int i=0;i<arr.length;i++) { if(arr[i] == target) { System.out.println("Item found at = "+(i+1)); } } return -1; } } }
Main, but still in the same file) or not create a separate class at all (all it does is contain a single method that could just as easily bestatic).