0

Possible Duplicate:
Causes of 'java.lang.NoSuchMethodError: main Exception in thread “main”'

i am new in java.i want to write a program to swap 2 nos.
i have written 2 programs on it.one is running and other is not.
i cant understand the fault of the not running program.pls help me to understand my fault.
here i giving you both the programs along with the output.

the running program:

public class SwapElementsExample {


public static void main(String[] args) {

int num1 = 10;
int num2 = 20;

System.out.println("Before Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);
swap(num1, num2); 
}

private static void swap(int num1, int num2) {  
int temp = num1;  
num1 = num2;  
num2 = temp;  

System.out.println("After Swapping");
System.out.println("Value of num1 is :" + num1);
System.out.println("Value of num2 is :" +num2);
}
}

the output is:

before swapping
value of num1 is : 10
value of num2 is : 20
after swapping
value of num1 is : 20
value of num2 is : 10

in the above mentioned program i have not any problem.
but in the next program what is the fault i cannot find.
pls help me to find the error.

class Swap  
{  
public static void main(int a, int b)  
{  
int c=0;  
c=b;  
b=a;  
a=c;  
c=0;  
System.out.println(a);  
System.out.println(b);  
}  
}  

in the execution there is no error msg.
but in runtime there is a error msg and that is:
exception in thread "main" java.nosuchmethoderror:main

pls let me know the problem of this program.


Community
  • 1
  • 1
rine
  • 1

3 Answers3

3

public static void main(int a, int b) is not correct.

It must be: public static void main(String[] args). This is by definition.

If you want to get the first and second argument:

int a = Integer.parseInt(args[0]);
int b = Integer.parseInt(args[1]);
Petar Minchev
  • 44,805
  • 11
  • 98
  • 117
0

problem lies here

public static void main(int a, int b) 

Java always starts executing program from the main method you declared in the first sample code.

Chandra Sekhar
  • 16,511
  • 14
  • 71
  • 109
0

When you start your java application, the java interpreter tries to find a method

public static void main(String[] args)

to run the application.

In one class you can declare several methods with the same name but with different parameters they get. Like that:

public static void main(String[] args) {

}

public static void main(int a, int b) {

}

public static void main(float a, float b) {

}

And all these methods will be accepted by compiler. Because every method is not identifying by its name only, but by its name and signature. Signature is based on the parameters you pass to a method. Type of every parameter and parameters sequence are the signature body.

So, when you start your app without public static void main(String[] args) method inside, then interpreter is unable to find the main method with expected signature String[] args, and throws the exception.