0

I have two packages inside my project, a and b.

a is my "main class" that runs when the program runs but I need to make b run from a (if that makes sense).

I'm sure its something along the lines of PackageB.BMain but I'm not sure.

Edit:

Okay so I've learned a few new things, to start my main project is RevIRC, inside that I have two packages, MainChat and RevIRC, now when I run the program RevIRC is ran, I need to make Mainchat run when RevIRC is ran.

Like I said before I'm sure its something along the lines of RevIRC.MainChat.ChatMain() but I can't seem to figure it out.

Tazmanian Tad
  • 193
  • 1
  • 2
  • 12
  • Welcome to Stack Overflow! We encourage you to [research your questions](http://stackoverflow.com/questions/how-to-ask). If you've [tried something already](http://whathaveyoutried.com/), please add it to the question - if not, research and attempt your question first, and then come back. –  Oct 22 '12 at 08:55
  • A Java class with a `main(String[])` would typically import and create instances of classes in other packages, and use them without calling any other `main` method. – Andrew Thompson Oct 22 '12 at 08:55
  • Its absolutely no problem if you are new to some language. We are here to help you. But first you need to show us what you have tried, and what problem are you facing? – Rohit Jain Oct 22 '12 at 08:56
  • You mean to say from A's main method you want to call B's main method? – Bhavik Shah Oct 22 '12 at 08:56
  • Does A and B both, have a main method? – Lews Therin Oct 22 '12 at 08:57
  • Bhavik, exactly! and LEws, yes they do – Tazmanian Tad Oct 22 '12 at 08:58
  • Do `RevIRC` & `Mainchat` both `extend JFrame`? If so, see [The Use of Multiple JFrames, Good/Bad Practice?](http://stackoverflow.com/a/9554657/418556) – Andrew Thompson Oct 22 '12 at 09:01

3 Answers3

1

You have 2 options:

  • Either create a new instance of B from A, like so: PackageB.BMain b = new PackageB.BMain();
  • Access the methods in BMain in a static way like so: PackageB.BMain.someMethod();`

Note that you can use either of these exclusively or mix them up together, however, it all depends on how you have written your BMain class.

So for instance:

package PackageB

public class BMain
{
    public BMain()
    { }

    public void foo()
    {
        System.out.println("This is not a static method. It requires a new instance of BMain to be created for it to be called");
    }

    public static void bar()
    {
        System.out.println("This is a static method. It can be accessed directly without the need of creating an instance of BMain");
    }
}

Then in your main class (the class which has the main method):

package PackageA

public class AMain
{

    public static void main(String[] args)
    {
        PackageB.BMain.bar();

        PackageB.BMain bInstance = new PackageB.BMain();
        bInstance.foo();
    }
}
npinti
  • 51,780
  • 5
  • 72
  • 96
  • @TazmanianTad: You're welcome. I would also recommend you follow some beginner Java tutorials. – npinti Oct 22 '12 at 09:08
0

If you have two main methods it will either run from A or B. The JVM will choose the first main method it sees IIRC.

Have a standalone class that will have main. And create your classes there.. ?

import a.Class1;
import b.Class2;
public class MainController
{

  public static void main(String args[])
  {
     Class1 class1 = new Class1() ;
     Class2 class2 = new Class2() ;
    //Both class no start at the "same" time.
  }
}
Lews Therin
  • 10,907
  • 4
  • 48
  • 72
0

if i am not wrong you want to run main method of class B from class A. That you can call using B.main(arg[]); eg :

package a;
public class A
{ 
public static void main(String[] args)
{
    System.out.println("This is main method of class A");
    B.main(null);
            /*pass any args if you want or simply set null arg*/
}
}
package b;
public class B
{
public static void main(String[] args)
{
    System.out.println("This is main method of class B");
}
}

i hope this simple example will clear your doubt.

you can refer to link which contains Java tutorial for beginners.

kamlesh patel
  • 375
  • 2
  • 13