I don't know how to call this method to another program.

public class ForLoopTest
{
  public static void main ( String [] args)
  {
    int product = 1;
    for ( int i = 1; i <= 10; i++ )
    {
      product *= i;
      
    }
    System.out.printf(" %d ", product);
  }
}

Recommended Answers

All 11 Replies

I don't know how to call this method to another program.

Do you mean from another program? If so what language is the other program written in? Usually a system call works but that's not the Java way.

why do not you name different instead of main. Since main is special purpose function in Java it is not possible to be called in different class. It is default to be executed when you execute your program.

commented: This is incorrect. Please check your facts before you assert them. +0

Do you mean from another program? If so what language is the other program written in? Usually a system call works but that's not the Java way.

is java. I know how to call it if it had a method name but since doesn't have one i can't seem to figure it out.

why do not you name different instead of main. Since main is special purpose function in Java it is not possible to be called in different class. It is default to be executed when you execute your program.

There is no way i can call it?

nothing impossible in programming :) but it is not the general and thinkable usage at least.

Do you mean recursively? That's the entry-point for the entire Java Application. I don't see why you couldn't call main() from main(), but a function call requires something to dereference (something to point at. What alone are you trying to call? The entire App?
I apologize for not entirely understanding your question. Will you word it differently?

I just want to call the for loop.

public class ForLoopTest
{
  public static void main ( String [] args)
  {
    int product = 1;
    
    System.out.printf(" %d ", forLoopTest(product));
  }

  public static int forLoopTest(int product)
   {     
     for ( int i = 1; i <= 10; i++ )
      {
        product *= i; 
      }
      return product;
}
}
public class ForLoopTest
{
  public static void main ( String [] args)
  {
    int product = 1;
    
    System.out.printf(" %d ", forLoopTest(product));
  }
 
  public static int forLoopTest(int product)
   {     
     for ( int i = 1; i <= 10; i++ )
      {
        product *= i; 
      }
      return product;
}
}

If you want to call a method of a class, you instantiate that class and call the method.

public class FLTRunner
{
public class FLTRunner
{
   public static void main(String[] args)
   {
      ForLoopTest flt = new ForLoopTest();
      flt.main(null);  //null because main requires a String array as an argument

   }
}

Since main is (by definition) a static class, you don't actually need to instantiate the class, so you can even save a line:

public class FLTRunner
{
   public static void main(String[] args)
   {
      ForLoopTest.main(null);
  
   }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.