• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Main Method

 
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
I keep getting main method errors. I have copied code and pasted it, but it still wont compile or run, can you see the problems please.
package com.ack.learning;

public interface AnInterface {
// can define constants in interfaces
public static final int MAX_VALUE = 300;

// but really use interfaces for declaring methods
public void aMethod();

public int anotherMethod( String hey ) throws Exception;

public void yetAnotherMethod() throws Exception, ArithmeticException;
}

class ImplementInterfaceClass implements AnInterface {

public void aMethod() {

}

// note that methods are matched on formal parameter types and
// method name.
public int anotherMethod( String yep ) throws Exception {
// note that when you implement an interface, its constants
// are brought into the defining class's scope
if( true ) {
return MAX_VALUE;
}
else {
// however, best to always prefix constants with the defining
// class or interface for readability purposes
return AnInterface.MAX_VALUE;
}
}

/**
* this causes a duplication definition of anotherMethod because
* the method signature only differs by the return type. In Java,
* if two method differ only by the return type they are the same method
* from an inheritance perspective
*
public String anotherMethod(String yep) throws Exception
{
return null;
}
*/

// finally, you can selectively choose which exception to declare
// on the throws clause of the implementing method, here we choose to omit
// Exception
public void yetAnotherMethod() throws ArithmeticException {

}

}
 
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wayne Styles:
Hi
I keep getting main method errors. I have copied code and pasted it, but it still wont compile or run, can you see the problems please...


Your code compiles fine for me. Could you post the errors you're getting?

Once it does compile... In order to run a program from a command line, you need a "main method" of the form...

public static void main(String[] args) {...}

This method is called when you enter "java SomeClass" at the command line, so whatever you put in the main method body is what actually runs.
[ August 21, 2006: Message edited by: marc weber ]
 
Wayne Styles
Ranch Hand
Posts: 45
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi
java.lang.NoSuchMethodError: main
Exception in thread "main"
 
Ranch Hand
Posts: 3061
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wayne Styles:
Hi
java.lang.NoSuchMethodError: main
Exception in thread "main"



How are you compiling and running this program? From my experience, if you got this message, then your code DOES compile. However, it cannot run because (as the error says) there is no method named "main". You need to create sucha method with the correct signature as marc describes above. Let us know if you need more help.

Layne
 
marc weber
Sheriff
Posts: 11343
Mac Safari Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Originally posted by Wayne Styles:
Hi
java.lang.NoSuchMethodError: main
Exception in thread "main"


This looks like it's compiling okay, but when you try to run, it just can't find a main method. See my post above (which I added to while you were responding).
 
reply
    Bookmark Topic Watch Topic
  • New Topic