0

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; } } } 
4
  • so that means this is fine right as it is only used in this class? Commented Sep 2, 2021 at 9:40
  • 2
    @DevWithZachary this isn't an inner class, it's a nested class (more). Inner classes are always non-static. Commented Sep 2, 2021 at 9:41
  • 1
    @AndyTurner your right i missed the static part and need way more coffee. Thank you for pointing it out Commented Sep 2, 2021 at 9:43
  • 1
    As long as your code is short like that most "code style" guidelines aren't really important. Most of them matter with bigger code bases. Personally I'd rather make this a non-public top-level class (i.e. on the same level as 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 be static). Commented Sep 2, 2021 at 9:43

1 Answer 1

2

If you create a class or a method it's because you need to use the same code elsewhere, if you need to do it I suggest you putting that code in a LinearSearch class file or transforming it into a public static method (with static methods you can call it without declaring the object, like Main.linearSearch(arr,target)) else if you need to use it only once as in your code you can transform it in a private method or simply putting the code you need instead of using a method.

Also, as i see, the returned value isn't used, so you can transform it into a void function.

Like this:

public class Main { public static void main(String[] args) { Scanner sc = new Scanner(System.in); 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(); linearSearch(arr, x); } private static void 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)); } } } } 
Sign up to request clarification or add additional context in comments.

2 Comments

So that means rather than using a Class or an inner class, I should use a function, since this code is a small one. I got it now, thank you so much!!! :))
Choosing between class or functions (in java terms, "method") often depends to the amount of code and data you have to deal... and other factors. Also I never loved using more than a class in a single file, and I think that is not the way Java is supposed to be used, so I suggest you not doing that.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.