可以通过使用类而不是main()函数来运行程序吗?

huangapple go评论73阅读模式
英文:

is it possible to run a program by using a class without main( )

问题

[![输入图片说明文字][1]][1]    我在某处阅读到你的Java程序在JVM上执行时总是从main方法开始执行
    但是当我在**bluej**上编译下面的程序时它成功地编译并且没有任何错误


    import java.util.*;
    class Calculate
    {
          int m,n,s,d;//成员变量
         void inputdata()
         {
             Scanner sc = new Scanner(System.in);
             System.out.println("输入两个数字");
             m  = sc.nextInt();
             n = sc.nextInt();
         }
         void calculation()
         {
             s = m+n;
             d = m-n;
         }
         void outputdata()
         {
             System.out.println("两个数字的和:"+s);
             System.out.println("两个数字的差:"+d);
         }
        
    }


请告诉我在下面的程序中没有main方法所以它如何在BLUEJ上成功编译我们需要创建一个main函数当应用程序首次运行时控制权会被转移到该函数否则JVM找不到要传递控制权的函数你的应用程序将无法运行
[![没有main方法的程序成功编译][2]][2]

Note: The above translation is based on your request to only return the translated portion and exclude any additional content. If you have any more text that needs translation or any questions, feel free to ask.

英文:

可以通过使用类而不是main()函数来运行程序吗? I've read somewhere Your Java program executing on the JVM will always begin it's execution with the main method.
but when i compile below program on bluej it successfully compile without any error.

import java.util.*;
class Calculate
{
      int m,n,s,d;//Data members
     void inputdata()
     {
         Scanner sc = new Scanner(System.in);
         System.out.println("enter two numbers");
         m  = sc.nextInt();
         n = sc.nextInt();
     }
     void calculation()
     {
         s = m+n;
         d = m-d;
     }
     void outputdata()
     {
         System.out.println("sum of two  numbers:"+s);
         System.out.println("difference of two numbers:"+d);
     }
    
}

Please tell me in below program there is not main method so how does it compile successfully on BLUEJ? we need to create a main function where the control would be transfered when your application is first run. Otherwise the JVM won't find a function to transfer the control and your app would not run.
可以通过使用类而不是main()函数来运行程序吗?

答案1

得分: 6

可以在没有"main"方法的情况下编译类,因为这是100%有效的Java。

您只是不能将该类用作应用程序的入口点。但是另一个应用程序可以在您的类的基础上构建。

编译和执行是两种不同的事情,理解这一点很重要。

英文:

You can compile a class without a "main" method because that is 100% valid Java.

You just can't use that class as the entry point of your application. But another application could build upon your class.

Compilation and execution are two different things and it's important to understand that.

答案2

得分: 0

主方法是程序的入口点,意味着每次运行程序时,其执行都始于主方法。因此,很快就不可能发生这种情况。Java简介。

英文:

The main method is the entry point to your program meaning any time you run your program its execution begins from the main method. So shortly no that cant happen.Introduction to java.

huangapple
  • 本文由 发表于 2020年6月29日 15:15:55
  • 转载请务必保留本文链接:https://go.coder-hub.com/62633014.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定