Link to home
Start Free TrialLog in
Avatar of rzvika2
rzvika2

asked on

Invoke other method than main(String[])

Hi!
If I specify to a java program a class to run it runs its "public static void main(String[])" method.
Is there a way to run other "public static void methodName(String[])" method?
Thanks!
Avatar of Mick Barry
Mick Barry
Flag of Australia image

No, that is the only entry point for a Java application.

You could simply write a wrapper class that calls your method:

public Wrapper
{
   public static void main(String[] args)
   {
      MyClass.methodName(args);
   }
}
Avatar of paku
paku

Yes you can run other public static void methodName(String[] arg) in same class.

for example..

public class test
{
     public static void mine(String[] args)
     {
          System.out.println("This is mine method");
     }

     public static void main(String[] args)
     {
          mine(args);
          System.out.println("This is main method");
     }
}

hope above code will help you..

1. that doesn't actually answer the question
2. it's pretty much copied from my comment.

Lets keep it original please :)
Avatar of rzvika2

ASKER

My big mistake...
I've ment that I want to run it from the comand line.
I don't want that the main method will call it.
My point is to create some methods - each one do different thing, and the user can invoke the relevant method directly from the commnad line.
BTW, I don't want to give the main(String[]) method parameter wich method to invoke...

Thanks.
> My point is to create some methods - each one do different thing, and the user can invoke the relevant
method directly from the commnad line.

  I think you have misunderstood something here. When you call a class you call it by name and then it is the Virtual Machine that looks for the main method. This is default and you cannot change it (as objects pointed out as well). No matter what the name of your class is, it is the main method that will be called every time. If you have a class:

public class MyClass
{
    public static void main(Strign [] arguments)
    {}
}

and another one:

public class MyClass2
{
    public static void main(String [] arguments)
    {
    }
}

  in order to run them you do:

"java MyClass" and "java MyClass2" and not "java main". In a few words you *cannot invoke methods from the command line. Just classes*. So if you want your users to call different "names" then have different class names that all of them will have a "main" method.

  Hope it helps.
ASKER CERTIFIED SOLUTION
Avatar of Mick Barry
Mick Barry
Flag of Australia image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of rzvika2

ASKER

girionis:
I've understood it and that's why I've asked this question.
The reason not to do different class for each purpose is because they should behave in a very common way with little differences.