English Deutsch Français Italiano Español Português 繁體中文 Bahasa Indonesia Tiếng Việt ภาษาไทย
All categories

4 answers

There can be only one true main() per class. Java requires method signatures (in this case "public static void main( String[] args )") to be unique.

You could declare a different main() with different arguments, but it would not be executed as the main method for that class.

You can declare multiple main() methods in multiple classes, one per class. If you have a family of related applications, this would be fairly common -- they share so much code that you'd package them all up together and then just run different classes' main() methods to run different things.

2007-08-18 02:43:06 · answer #1 · answered by McFate 7 · 4 0

You can have multiple main method but you can have only one

static main method which takes String[] as argument.

public static void main(String[] argv)


The rest would be treated as normal method.

:)

2007-08-20 22:14:37 · answer #2 · answered by Zeus 3 · 0 0

Java is based on OOP concepts, i.e. Encapsulation,Inheritance and Polymorphism.

Polymorphism poly means many form. i.e.One interface multiple
methods.

Under that comes Overloading and overriding

overloaded methods:

1. appear in the same class or a subclass
2. have the same name but,
3. have different parameter lists, and,
4. can have different return types

* an example of an overloaded method is print() in the java.io.PrintStream class

public void print(boolean b)
public void print(char c)
public void print(char[] s)
public void print(float f)
public void print(double d)
public void print(int i)
public void print(long l)
public void print(Object obj)
public void print(String s)

* the actual method called depends on the object being passed to the method
* Java uses late-binding to support polymorphism; which means the decision as to which of the many methods should be used is deferred until runtime

Overriding methods

* late-binding also supports overriding
* overriding allows a subclass to re-define a method it inherits from it's superclass
* overriding methods:
1. appear in subclasses
2. have the same name as a superclass method
3. have the same parameter list as a superclass method
4. have the same return type as as a superclass method
5. the access modifier for the overriding method may not be more restrictive than the access modifier of the superclass method
o if the superclass method is public, the overriding method must be public
o if the superclass method is protected, the overriding method may be protected or public
o if the superclass method is package, the overriding method may be packagage, protected, or public
o if the superclass methods is private, it is not inherited and overriding is not an issue
6. the throws clause of the overriding method may only include exceptions that can be thrown by the superclass method, including it's subclasses

class LBException extends Exception {}
class LBException1 extends LBException {}

In superclass:
public void testEx() throws LBException {
throw new LBException();
}

In subclass:

public void testEx() throws LBException1 {
throw new LBException1();
}

* overriding is allowed as LBException1 thrown in the subclass is itself a subclass of the exception LBException thrown in the superclass method

Side effect of late-binding

* it is Java's use of late-binding which allows you to declare an object as one type at compile-time but executes based on the actual type at runtime

class LB_1 {

public String retValue(String s) {
return "In LB_1 with " + s;
}

}

class LB_2 extends LB_1 {

public String retValue(String s) {
return "In LB_2 with " + s;
}

}

* if you create an LB_2 object and assign it to an LB_1 object reference, it will compile ok
* at runtime, if you invoke the retValue(String s) method on the LB_1 reference, the LB_2 retValue(String s) method is used, not the LB_1 method

LB_2 lb2 = new LB_2();
LB_1 lb3 = lb2; // compiles ok

System.out.println(lb3.retValue("Today"));

Output:
In LB_2 with Today

===========================

This is you answer

public class MainNotSpecial {
public static void main() {
System.out.println("In no-argument main");
}

public static void main(int argc, String argv[]) {
System.out.println("In int,String[] main");
}
public static void main(String argv) {
System.out.println("In String main");
}

public static void main(String[] argv) {
System.out.println("In String[] main");
}
}

/** If main is not special to the compiler, you should be able to
* write it any way you like. And you can. But the java
* command only knows to call one particular form, as you can see
* by compiling and running this program.
*/

another example

class A extends Object{
public static void main(
String[] args){}
public void m(){
System.out.println("m()");
}//end method m()
}//end class A
//===================================//

class B extends A{
public void m(int x){
System.out.println("m(int x)");
}//end method m(int x)
//---------------------------------//

public void m(String y){
System.out.println("m(String y)");
}//end method m(String y)
}//end class B
//===================================//

public class Poly01{
public static void main(
String[] args){
B var = new B();
var.m();
var.m(3);
var.m("String");
}//end main
}//end class Poly01
//===================================//

output
---------- java ----------
m()
m(int x)
m(String y)

Output completed (4 sec consumed) - Normal
=================================

public class dd {


public static void main(String[] args) {

dd d = new dd();
String[] arr = new String[2];
d.main(arr, "s");
}

public static void main(String[] args,String s) {
// TODO Auto-generated method stub
System.out.println("hi");
}

}

========================

Now you try to write meaning full mehtod by your self

good luck

2007-08-18 06:24:58 · answer #3 · answered by angel04 3 · 0 0

If I understand what you are asking, you could either use an array and pass the values through that, or use a string then Integer.parseInt() or Double.parseDouble() to get the values out.

2016-03-17 01:51:12 · answer #4 · answered by Anonymous · 0 0

no it cannot. the main method defines what part of the program is to be executed. to create such an effect create different methods then call them in the main method.

2007-08-18 02:36:30 · answer #5 · answered by rodette p 3 · 0 1

fedest.com, questions and answers