Can a main() method of class be invoked from another class in java

how to call a class from main class in java
how to call a method in java from another class
how to call a class inside another class in java
how to call a method from another class in same package java
how to call a method from another class file in java
how to call main method from another method in java
how to call a class from another class in java
how to call a method in main class in java

Can a main() method of class be invoked in another class in java?

e.g.

class class1{

  public static void main(String []args){

  }

}

class class2{

  public static void main(String []args){
      class1.main();
  }

}

If you want to call the main method of another class you can do it this way assuming I understand the question.

  public class MyClass {

    public static void main( String[] args ) {

      System.out.println("main() method of MyClass");
      OtherClass obj=new OtherClass();
    }
  }

  class OtherClass {

    public OtherClass() {

      // Call the main() method of MyClass
      String[] arguments = new String[] {"123"};
      MyClass.main(arguments);
    }
  }

Can a main() method of class be invoked from another class in java , Then declare a reference variable of the target class type (whose method you want to call. class then you can invoke main method like you call the other static methods. Why is the main() method in Java written with access modifier? Though Java doesn’t prefer main () method called from somewhere else in the program, it does not prohibit one from doing it as well. So, in fact, we can call the main () method whenever and wherever we need to. But calling the main () method from our code is tricky. It can lead to many errors and exceptions, such as:

if I got your question correct...

main() method is defined in the class below...

public class ToBeCalledClass{

   public static void main (String args[ ]) {
      System.out.println("I am being called");
   }
}

you want to call this main method in another class.

public class CallClass{

    public void call(){
       ToBeCalledClass.main(null);
    }
}

How does one main method be called in another class main method , Java - Can a main() method of class be invoked in another class . It is possible. we can easily invoke a main() of a class from another class. In this blog post, I will show you how to call a main from inside a main method in java using reflection Let’s start from creating a simple class Hello.java as shown below, Now, Let’s create another class Test.java which also has a main method and let’s call the main from Hello here, Optionally you […]

yes, but only if main is declared public

Can a main() method of class be invoked in another class, How to call private method from another class in java? Reflection API newInstance() method javap tool creating javap tool creating You can call the private method from outside the class by changing the runtime Method;; public class MethodCall{; public static void main(String[] args)throws Exception{; Class c = Class. Java Questions 1. Can a main() method of class be invoked in another class? 2. What is the difference between java command line arguments and C command line arguments? 3. What is the difference between == & .equals 4. What is the difference between abstract class & Interface. 5. What is singleton class & it's implementation. 6. Use of static

Yes as long as it is public and you pass the correct args. See this link for more information. http://www.codestyle.org/java/faq-CommandLine.shtml#mainhost

how to invoke private method from outside the class, What happens inside JVM when you invoke main() method? windows) use Java Native Interface (JNI) calls to invoke method, so they can have invoked it A second way to answer this is another question, why not public? Error: Main method is not static in class main, please define the main method as:. If static as a keyword is not there, then the method can only be invoked through an object. E.g., if the class was called ExampleObject and is also had a constructor, then a new object could be made by typing ExampleObject obj = new ExampleObject ( ); and method will be called with “obj.methodExample ( .”.

As far as I understand, the question is NOT about recursion. We can easily call main method of another class in your class. Following example illustrates static and calling by object. Note omission of word static in Class2

class Class1{
    public static void main(String[] args) {
        System.out.println("this is class 1");
    }    
}

class Class2{
    public void main(String[] args) {
        System.out.println("this is class 2");
    }    
}

class MyInvokerClass{
    public static void main(String[] args) {

        System.out.println("this is MyInvokerClass");
        Class2 myClass2 = new Class2();
        Class1.main(args);
        myClass2.main(args);
    }    
}

Output Should be:

this is wrapper class

this is class 1

this is class 2

public static void main (String[] args), The short answer to, can we overload the main method in Java is Yes, you can Though main() is a special method because its the entry point of your Java class Helloworld { /** * Standard main method, JVM will only call this method Another overloaded main method which accepts Double[] instead of * String[] as � The main method is officially considered to be a starting point of execution by JVM. But when code execution doesn't need to be directly initiated from that class, no difference is there in the working of main or other static methods. 7.6K views View 1 Upvoter

Can you Overload or Override main method in Java?, Static methods use no instance variables of any object of the class they are You can access static variables, but except for constants, this is unusual. The above mean() method would work just as well if it wasn't declared static Called from outside the class: If a method (static or instance) is called from another class, � You can have one public static void main (String [] args) method in each class. Some people use those methods for testing. They can test the operation of each class individually. The JVM will only invoke the public static void main (String [] args) method in the class you name when you write java MyClass.

Java: Static/Class methods, In this chapter, you will learn to design classes that represent useful objects. Another example, which we will implement ourselves, is Time , which represents a time of day. public static void main(String[] args) { Time time = new Time(); }. When you use new , Java creates the object and invokes your constructor to� In case of java,The Java Virtual Machine (JVM) starts up by creating an initial class, which is specified in an implementation-dependent manner, using the bootstrap class loader. The Java Virtual Machine then links the initial class, initializes it, and invokes the public class method void main (String []).

Designing Classes | Think Java, Before we see how, let's add another class to the project. We can then put all the methods there, instead of clogging up the main class. (You'll start learning more� 5) In order to use the Car class and its methods, we need to create an object of the Car Class. 6) Then, go to the main() method, which you know by now is a built-in Java method that runs your program (any code inside main is executed). 7) By using the new keyword we created a Car object with the name myCar.

Comments
  • for those who may or may not notice... this will create an infinite loop
  • @mmcrae This is recursion, not loop. Recursion is never infinite.
  • wait what...why would recursion not be infinite? You could blow your stack, but that is just a practical limitation, mathematical recursion can of course be infinite if you never make progress towards a base case (or no base case exists).
  • @JPC This is not mathematical recursion. It is computer programming. All resources are finite.
  • great.i was wondering what to put as main argument ! simply it was null
  • ... or protected, or package private, or you do tricky things with reflection to allow it to be invoked despite being private.
  • At least don't compare Strings with == (or !=).