IT TIP

Java에서 main 메소드를 오버로드 할 수 있습니까?

itqueen 2020. 11. 19. 22:46
반응형

Java에서 main 메소드를 오버로드 할 수 있습니까?


main()Java 에서 메서드를 오버로드 할 수 있습니까 ?


당신은 할 수 과부하 main()방법을,하지만이 public static void main(String[] args)클래스가 JVM에 의해 시작될 때 사용됩니다. 예를 들면 :

public class Test {
    public static void main(String[] args) {
        System.out.println("main(String[] args)");
    }

    public static void main(String arg1) {
        System.out.println("main(String arg1)");
    }

    public static void main(String arg1, String arg2) {
        System.out.println("main(String arg1, String arg2)");
    }
}

즉 것입니다 항상 인쇄 main(String[] args)실행할 때 java Test ...명령 줄에서 하나 또는 두 개의 명령 줄 인수를 지정하는 경우에도,.

main()물론 코드에서 직접 메서드 를 호출 할 수 있습니다. 이때 일반 오버로딩 규칙이 적용됩니다.

편집 : JVM 관점에서 동일하므로 varargs 서명을 사용할 수 있습니다.

public static void main(String... args)

예, Java에서 main 메소드를 오버로드 할 수 있습니다. 그러나 프로그램은 프로그램을 실행할 때 오버로드 된 메인 메소드를 실행하지 않으므로 실제 메인 메소드에서 오버로드 된 메인 메소드를 호출해야합니다.

즉, 메인 메소드가 자바 인터프리터가 애플리케이션 실행을 시작하는 진입 점 역할을한다는 것을 의미합니다. 로드 된 메인은 메인에서 호출되어야합니다.


예, 메서드 오버로딩으로. 메서드 오버로딩을 통해 클래스에 원하는 수의 기본 메서드를 가질 수 있습니다. 간단한 예를 보겠습니다.

class Simple{  
  public static void main(int a){  
  System.out.println(a);  
  }  

  public static void main(String args[]){  
  System.out.println("main() method invoked");  
  main(10);  
  }  
}  

다음 출력이 제공됩니다.

main() method invoked
10

예, main ()을 오버로드 할 수 있습니다.

그러나 명확하게 말하자면 main을 오버로드 할 수 있지만 표준 서명 이있는 버전 만 명령 줄에서 응용 프로그램으로 실행 가능합니다. 예 :

public static void main(String[] args,int a){
// some code
}
2)public static void main(String[] args){//JVM will call this method to start 
// some code 
}

예, Java에서 main 메소드를 오버로드 할 수 있습니다. 실제 main 메소드에서 오버로드 된 main 메소드를 호출해야합니다.


예, 주요 방법이 오버로드 될 수 있습니다. 오버로드 된 main 메소드는 JVM에 의해 클래스가 시작될 때 진입 점이므로 "public static void main (String args [])"내부에서 호출되어야합니다. 또한 오버로드 된 주 메서드는 일반 메서드와 마찬가지로 모든 한정자를 가질 수 있습니다.


그래 넌 할수있어.

mainJava 방법은 외계 방법이 아닙니다. main()다른 메소드와 마찬가지로 유사한 방식으로 오버로드 될 수 있다는 사실 외에도 JVM은 항상 프로그램을 시작하기 위해 메소드 서명을 찾습니다.

  • 일반적인 main방법은 JVM이 프로그램 실행을 시작하기위한 진입 점 역할을합니다.

  • mainJava 에서 메서드를 오버로드 할 수 있습니다 . 그러나 프로그램은 프로그램을
    실행할 때 오버로드 된 main메서드를 실행 하지 않습니다 main. 실제 메인 메서드에서만 오버로드 된 메서드 를 호출해야 합니다.

    // A Java program with overloaded main()
    import java.io.*;     
    public class Test {         
      // Normal main()
      public static void main(String[] args) {
        System.out.println("Hi Geek (from main)");
        Test.main("Geek");
      }     
      // Overloaded main methods
      public static void main(String arg1) {
        System.out.println("Hi, " + arg1);
        Test.main("Dear Geek","My Geek");
      }
      public static void main(String arg1, String arg2) {
        System.out.println("Hi, " + arg1 + ", " + arg2);
      }
    }
    

    Java에서 유효한 main () 변형


네, 우리는 메인 메소드를 오버로드 할 수 있습니다. 주 메서드는 정적 주 메서드가 아니어야합니다.


이것은 완전히 합법적입니다.

public static void main(String[] args) {

}

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

예. 'main ()'메서드는 오버로드 될 수 있습니다. 귀하의 질문에 답하기 위해 코드를 입력하려고했습니다.

public class Test{
static public void main( String [] args )
        {
                System.out.println( "In the JVMs static main" );
                main( 5, 6, 7 );    //Calling overloaded static main method
                Test t = new Test( );
                String [] message  = { "Subhash", "Loves", "Programming" };
                t.main(5);
                t.main( 6, message );
        }

        public static void main( int ... args )
        {
                System.out.println( "In the static main called by JVM's main" );
                for( int val : args )
                {
                        System.out.println( val );
                }
        }

        public void main( int x )
        {
                System.out.println( "1: In the overloaded  non-static main with int with value " + x );
        }

        public void main( int x, String [] args )
        {
                System.out.println( "2: In the overloaded  non-static main with int with value " + x );
                for ( String val : args )
                {
                        System.out.println( val );
                }
        }
}

산출:

$ java Test
In the JVMs static main
In the static main called by JVM's main
5
6
7
1: In the overloaded  non-static main with int with value 5
2: In the overloaded  non-static main with int with value 6
Subhash
Loves
Programming
$

위의 코드에서 정적 메서드와 기본 메서드의 비 정적 버전은 모두 데모 용으로 오버로드됩니다. JVM을 main으로 작성하면 JVM이 프로그램을 실행하기 위해 먼저 사용하는 주요 방법입니다.


Yes,u can overload main method but the interpreter will always search for the correct main method syntax to begin the execution.. And yes u have to call the overloaded main method with the help of object.

class Sample{
public void main(int a,int b){
System.out.println("The value of a is "  +a);
}
public static void main(String args[]){
System.out.println("We r in main method");
Sample obj=new Sample();
obj.main(5,4);
main(3);
}
public static void main(int c){
System.out.println("The value of c  is"  +c);
}
}

The output of the program is:
We r in main method
The value of a is 5
The value of c is 3

Yes According to my point of view, we are able to overload the main method but method overloading that's it. For Example

class main_overload {
    public static void main(int a) {
        System.out.println(a);
    }
    public static void main(String args[]) {
        System.out.println("That's My Main Function");
        main(100);
    }
}

In This Double Back slash Step, I am just calling the main method....


Yes a main method can be overloaded as other functions can be overloaded.One thing needs to be taken care is that there should be atleast one main function with "String args[] " as arguments .And there can be any number of main functions in your program with different arguments and functionality.Lets understand through a simple example:

Class A{

public static void main(String[] args)
{
System.out.println("This is the main function ");
A object= new A();
object.main("Hi this is overloaded function");//Calling the main function
}

public static void main(String argu)     //duplicate main function
{
System.out.println("main(String argu)");
}
}

Output: This is the main function
Hi this is overloaded function


Yes you can Overload main method but there should be only one method with signature public static void main(string args[])

package rh1;

public class someClass 
{

    public static void main(String... args)
    {
        System.out.println("Hello world");

        main("d");
        main(10);
    }
    public static void main(int s)
    {

        System.out.println("Beautiful world");
    }
    public static void main(String s)
    {
        System.out.println("Bye world");
    }
}

참고URL : https://stackoverflow.com/questions/3759315/can-we-overload-the-main-method-in-java

반응형