Results 1 to 8 of 8
  1. #1
    eva
    eva is offline Member
    Join Date
    Dec 2007
    Posts
    49
    Rep Power
    0

    Default Calling main method

    How can I call main method from a method defined in some other class. Normally, main method is the starting point of the program and is not called explicitly.

    - PEACE

  2. #2
    Join Date
    Dec 2007
    Posts
    24
    Rep Power
    0

    Default

    yeh there is no way to call main function of one class 2 another , u prepare that code in the main in one method as some
    class c1
    {
    public void call()
    {

    }
    }

    inside this method u write the code..

    in another class u call the object of that class & call that method like
    class c2
    {
    public static void main(..)
    {
    C1 o=new C1;
    o.call;
    }
    }

  3. #3
    Jevy is offline Member
    Join Date
    Oct 2008
    Posts
    3
    Rep Power
    0

    Default

    public class c1{
    public void call(){

    }
    }

    //inside this method u write the code..

    //in another class u call the object of that class & call that method like
    public class c2{
    public static void main(String[] argv){
    C1 o = new C1();
    o.call;
    }
    } // end of c2 class

  4. #4
    dswastik is offline Senior Member
    Join Date
    Dec 2008
    Location
    Kolkata
    Posts
    282
    Rep Power
    6

    Default

    Should be in the same way, you call any other method

    Java Code:
    class One{
    	public static void main(String arg[]){
    		System.out.println("Main of one called...");
    	}
    }
    
    class Two{
    	public static void main(String args[]){
    		Two t=new Two();
    		t.callMainOfOne();
    	}
    
    	void callMainOfOne(){
    		String x[]={"A","B"};
    		One.main(x);
    	}
    }

  5. #5
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    Why not remove that code from the main method and put it into its own method. That method can then be called by the main method or any other methods.
    No need to call the main from other methods anymore.

  6. #6
    raqman is offline Member
    Join Date
    Sep 2009
    Posts
    37
    Rep Power
    0

    Default

    Try this ...
    ClassB {
    public static void main(String[] args) {
    System.out.println("ClassB main() Called");
    }
    }

    ClassA {
    public static void main(String[] args) {
    System.out.println("ClassA main() Called");
    ClassB.main(args);
    }
    }

    ouput
    ClassA main() Called
    ClassB main() Called

    static methods are called without instantiating the object of class containing the method.
    RAQ Report: free Java reporting tool.

  7. #7
    Obula is offline Member
    Join Date
    Nov 2009
    Posts
    1
    Rep Power
    0

    Smile Thank You for your best answer

    Quote Originally Posted by dswastik View Post
    Should be in the same way, you call any other method

    Java Code:
    class One{
    	public static void main(String arg[]){
    		System.out.println("Main of one called...");
    	}
    }
    
    class Two{
    	public static void main(String args[]){
    		Two t=new Two();
    		t.callMainOfOne();
    	}
    
    	void callMainOfOne(){
    		String x[]={"A","B"};
    		One.main(x);
    	}
    }

    //The same can be implemented in the main class too.
    class One{
    public static void main(String arg[]){
    System.out.println("Main of one called...");
    String x[]={"A","B"};
    One.main(x); //like forever loop
    }
    //The above code is also used for correct input of choice

    class One{
    public static void main(String arg[])
    {
    try{
    System.out.println("\t1. Addition\n\t 2. Substraction");
    System.out.print("Enter Your choice:");
    int n=scan.nextInt();//Have to read only integers
    }
    catch(Exception e)
    {
    System.out.println("ERROR : "+e.getMessage());
    System.out.println("\n\n\tInvalid choice\n\tPlease Try again");
    String x[]={"A","B"};
    Main.main(x);
    }

  8. #8
    RamyaSivakanth's Avatar
    RamyaSivakanth is offline Senior Member
    Join Date
    Apr 2009
    Location
    Chennai
    Posts
    843
    Rep Power
    6

    Default

    Please go thru sun tutorial for fundas.Please don't work with assumptions.Try to learn each and evrything properly before implementation
    Ramya:cool:

Similar Threads

  1. A query about main method
    By mew in forum New To Java
    Replies: 2
    Last Post: 12-24-2007, 10:44 AM
  2. Private main method
    By bugger in forum New To Java
    Replies: 1
    Last Post: 12-21-2007, 10:45 AM
  3. main method
    By eva in forum New To Java
    Replies: 5
    Last Post: 12-19-2007, 10:25 AM
  4. Calling a variable from main to another class
    By itsme in forum New To Java
    Replies: 1
    Last Post: 12-18-2007, 04:35 PM
  5. calling array from main
    By nalinda in forum New To Java
    Replies: 1
    Last Post: 11-17-2007, 10:41 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •