DaniWeb IT Discussion Community

Code Snippets (http://www.daniweb.com/code/)
-   java (http://www.daniweb.com/code/java.html)
-   -   How to execute a main method from Interface (http://www.daniweb.com/code/snippet756.html)

lookof2day java syntax
Aug 31st, 2007
Well any Java programmer knows that a Java Interface only allows declaration of methods. You cannot define the methods inside interfaces.

Yet if somebody like me wants you to define a method in interface and wants the program gets compiled and runs successfully, what would you do?

Well the answer is Inner Classes in Java. Through Inner classes, you can define a method in interfaces and run your programs.

Here's what you need to do:

  1. public interface IMain{
  2. public class Main{
  3. public static void main(String [] args){
  4. System.out.println("The main is defined inside an Interface". Don't believe me? Look at the source.. Geee..");
  5. }
  6. }
  7. }
  8.  
  9. compile the program using javac.
  10. run the program by typing java IMain$Main.
  11.