Question

Can you write a method inside the main method? For example I found this code:

public class TestMax {
    public static void main(String[] args) {
    int i = 5;
    int j = 2;
    int k = max(i, j);
    System.out.println("The maximum between is " + k);
}

 public static int max(int num1, int num2) {
    int result;
    if (num1 > num2)
       result = num1;
    else
       result = num2;

    return result; 
  }
}

Can the method max be coded inside the main method?

Was it helpful?

Solution 2

When Java 8 comes out, the Closure/Lambda functionality should make it so that you can define the max method in the main method. Until then, you'll only be able to define a method in the main method in special circumstances.

As it happens, your question does fall into the special circumstance. There is an interface (Comparable) which encapsulates the logic of comparing two things of the same type. As a result, the code could be rewritten as follows:

public class TestMax {
  public static void main(String[] args) {
    int i = 5;
    int j = 2;
    Comparator<Integer> compare = new Comparator<Integer>() {
        @Override
        public int compare(Integer o1, Integer o2) {
            // Because Integer already implements the method Comparable,
            // This could be changed to "return o1.compareTo(o2);"
            return o1 - o2;
        }
    };
    // Note that this will autobox your ints to Integers.
    int k = compare.compare(i, j) > 0 ? i : j;
    System.out.println("The maximum between is " + k);
  }
}

This only works because the comparator interface already exists in the standard Java distribution. The code could be made better through the use of libraries. If I were writing this code, I would add Google Guava to my classpath. Then I could write the following:

public class TestMax {
  public static void main(String[] args) {
    int i = 5;
    int j = 2;
    // The 'natural' ordering means use the compareTo method that is defined on Integer.
    int k = Ordering.<Integer>natural().max(i, j);
    System.out.println("The maximum between is " + k);
  }
}

I suspect that your question was more about the abilities of the Java language rather than standard practices having to do with ordering numbers (and other things). So this might not be useful, but I thought I'd share just in case.

OTHER TIPS

No, you can't declare a method inside another method.

If you look closely at the code you provided it is just a case of bad formatting, the main method ends before the max method is declared.

If you want to use it, in this scenario, make it static and place it in the class but outside the main method (as you have it in your code). Then you can call it from within main().

you cannot directly define methods inside other methods in Java ( main method too).

You should write the method in the same class as main. 

Note: You could declare a class with methods inside another method

With proper formatting, you will notice that the max method appears in the same class as the main method, but it is not IN the main method.

Yes, this is possible by lembda expression: for that you should use at least java 8

import java.util.function.BiFunction;

public class TestMax {
    public static void main(String[] args) {
        BiFunction<Integer, Integer, Integer> max = (a,b)-> a>b ? a : b;

        //now you can call method

        int i = 5;
        int j = 2;
        int k = max.apply(i, j);
        System.out.println("The maximum between is " + k);
    }
}

BiFunction is an functional interface java compiler internally converts lambda expression code as an anonymouse class which implements BiFunction interface and overrides apply() method which contains your code

Internal (compiler considered) code is:

BiFunction<Integer, Integer, Integer> max = new BiFunction<Integer, Integer, Integer>(){
    public Integer apply(Integer a, Integer b){
        return a>b ? a : b; //your code
    }
}

No. One cannot directly define method in methods in java. Except special conditions, like pre declared methods in inbuilt classes. The preferable method is define method in same class and ofcourse call it.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top