Main method in mulitple classes?

I'm new to java as some of you might know, and I've been working on a single class. Now I'm trying to add another class. I've tried many ways to do this, and I think I almost have it, but I'm still a little stuck. Here are a few questions:
1. I only can have one main method, which goes in the main class, right? Then the subclasses just have methods? So, how does the subclass know which methods to call first, and when? If there is a main method in the subclasses, t should be private and the main method in the main class should be public? So if I have multiple methods in a subclass, how do I tell my main method which to call? I've tried:
new Timer.test();
new Timer(test);Neither seem to work.
So, organized in a tree it should be like so:
Public class mainClass
     public main method
          new subClass
  Private class subClass
      (no main method?)
          private subClassMethod1
          private subClassMethod2I've tried searching, but maybe I'm not searching for the right terms. I can find things on creating classes and methods, but not really on the way multiple classes are set up. Can someone explain so I understand exactly what I'm doing with multiple classes, or give me a link to a good explanation? Thanks
Also, the subclass ALWAYS extends mainClass right?
Edited by: LIVE3A17 on Apr 28, 2009 3:13 PM

LIVE3A17 wrote:
Ok, so I can use main methods in the other classes, and java knows which is the main class by which one is opened when the program is run. Got it. Don't even put a main method in the other classes, unless you intend on one of them to be an entry point. Otherwise it's just code bloat.
>
Now, I have a main method in both classes. When I have the main method in the main class set to public, I get an error in the second class saying "Cannot reduce the visiblity of the inherited method". I gives me the option to change visibilty of my main class to private. However, when I do this, and try to run the main class, I get an error saying main method not public.I don't know what you're talking about. Sounds like you are subclassing something and for whatever reason trying to 'override' the main method. The normal static main method isn't even inherited, being static. So this error you're speaking of doesn't make much sense. Of course without seeing your code, we're left to mindreading exercises which usually don't go so well.

Similar Messages

  • How do you call a java class from the main method in another class?

    Hi all,
    How do you call a java class from the main() method in another class? Assuming the two class are in the same package.
    Thanks
    SI
    Edited by: okun on May 16, 2010 8:40 PM
    Edited by: okun on May 16, 2010 8:41 PM
    Edited by: okun on May 16, 2010 8:47 PM

    georgemc wrote:
    To answer your impending question, either the method you're calling has to be static, or you need an instance of that other class to invoke it against. Prefer the latterAnd to your impending question after that: no, don't use the Singleton pattern.

  • Can we call main method in another class?

    Hi...
    can we call main method in another class?
    If no, please tell me the exact reason why can't we call that....

    ok
    can u give that code for me?
    class A {
    public static void main(String [] args){
    System.out.println("In A");
    class B {
    public static void main(String [] args){
    A.main(null);
    }

  • How to call main method in one class from another class

    Suppose i have a code like this
    class Demo
    public static void main(String args[])
    System.out.println("We are in First class");
    class Demo1
    public static void main(String args[])
    System.out.println("We are in second class");
    In the above program how to call main method in demo calss from Demo1 class......???????

    No, i dont know how to call other than main methods from other classes
    And one more doubt i have i.e. Like in C can we see the execution of the program like how the code is being compiled step by step and how the value of the variable changes from step to step like(Add Watch).........

  • Invoking the main() method of a class within another

    Greetings -
    Can someone tell me what happens when you invoke the main() method of a class from within another class?
    I have a third-party class, designed to be invoked from a command line, that I want to invoke from within a separate class. What appears to be happening is that following the successful execution of the third-party class, the invoking class is terminated as well. That's not the intended behavior I had envisioned. ^_^
    Is there a better way than invoking the main() method? I still need the invoking class to continue executing.
    Thanks!

    Navigate yourself around pitfalls related to the Runtime.exec() method

  • Calling main method of a class

    Hi
    I have to call the Main method of a class in some other class becuass that class is accepting command line aurguments.
    I test it and everything is working fine.
               String[] strings = new String[3];
               strings[0] = "E:/practice.cap";
               strings[1] = "-o";
               strings[2] = "E:/practice.txt";
               Main.main(strings);I now want to know is there any drawback of doing this ?
    BR
    regards

    Muhammad Umer wrote:
    Good Explanation. But my question is not to explain the importance of main method in classes :)As far as the Java language and APIs go, there's nothing wrong with calling main just like any other method, because it is just another method. It's only special to the JVM, and to programmers who assume that it's the entry point to the program.
    As far as Java is concerned, you can call as many mains as you want. The only potential problem arises if some particular main is written in a way that relies on it being the entry point to the whole program. That is, if the author wrote it with the assumption that it would only be called by the JVM at startup time, and would not be called arbitrarily from within your code (EDIT: and if proper behavior relies on that assumption being upheld). However, this is just a particular case of a general issue that we always have to be aware of, namely, make sure you understand how a method is intended to be used, and use it in that fashion, or accept the consequences.
    EDIT: However, having said that, it's unusual to call main explicitly, so if you can, I would suggest pulling the functionality that you want out of that main into a different method, and then both your code and the main can call that method. If you cannot do that, then at the point where you call main, clearly document why you're doing it. Otherwise, whoever reads the code a few months or years from now--maybe somebody else, maybe you--will be wondering if the programmer really knew what he was doing, or if that is a mistake. That kind of second-guessing is the source of a lot of maintenance nightmares.
    Edited by: jverd on Nov 3, 2011 8:43 AM

  • Running a main Method from another class??

    Hi,
    I am trying to run a main method from another class, eg the main method is in Class1 and i am trying to run it from class2.
    So I have
    class1 c1 = new class1();
    c1.main();and I get the following compilation error:
    clas2.java:42: main(java.lang.String[]) in class1 cannot be applied to () c1.main();
    any ideas on how to do this correctly
    thanks in advance,
    Donal

    Hi thanks for the replies,
    tried just passing a string earlier and that just gave errors too, I should have been more specific and pass a string array.
    Its working now thanks again.
    Donal

  • Calling main method in another class using command line arguements

    Hi
    My program need to use 4 strings command line arguments
    entered in Project properties/Run/Application Parameters
    java programming for beginners // arguments
    The program needs to call main in the second class 4 times using argument 1 the first call, argument 2 the second call on so on.
    So the output wil be
    java
    programming
    for
    beginners
    import java.lang.*;
    public class First extends Second{
      public static void main(String[] args) {
        for(int i = 0; i < args.length; i++){
           Second.main(args); // Error I think
    import java.lang.*;
    public class Second {
    public static void main(String[] args){
    System.out.println(args[0]);
    "First.java": Error #: 300 : method main(java.lang.String) not found in class Second at line 6, column 15
    I am only a beginner with little knowledge of java so can the code be as basic as possible

    Your style looks quite bad for starters..... Hows
    this://import java.lang.*; /* NOT NEEDED */
    public class First extends Second{
    public First(String s) {
    super(s);
    public static void main(String[] args) {
    for(int i = 0; i < args.length; i++){
    new First(s);
    public class Second {
    public Second(String s)
    System.out.println(s);
    NOT NEEDED:
    public static void main(String[] args){
    System.out.println(args[0]);
    }My question to you: Do you understand why my code
    works? (does it do what you want?)I think since this is some kind of lesson, the OP have to implement some way to use the main method of the Second class as it is, that is, with String[] args. I think this lesson is interesting exactly because of this requirenment. But, anyway, I don�t know, that is just my assumption...

  • Public main method in a non-public class

    Hi,
    It is said that main method in a java class must be public so that it can be invoked by JVM. This works even if the class itself has default modifier.
    But while programming, any method (be it public) of a class with default access modifier from one package can't be accessed by classes from other packages. For example,
    1. Consider class A in package a.
    package a;
    class A {
    public void methA() {}
    2. Now consider class B in pakage b.
    package b;
    class B {
    void methB() {
    // Here it can't access class A and its method methA()
    So how does JVM invoke the main method of a class with default access modifier. Assume that the class is in some package a.b.c
    Regards,
    Amit

    Hi,
    Maybe this can also be beneficial for you.
    Standalone applications don't necessarily have init and start methods. They must have a public static void main routine to start things off. The main routine typically creates some object of the master class. It can't use any non-static variables or methods in the class with the implied this, since there is no this object. It typically quickly returns. Then the event loop processing starts. Even the static main method must live inside some public class, or at least in the top class, the first class and the class for whom the source file is named. There are no such thing as standalone static methods in Java. The signature of the main class must look exactly like this. Cut and paste it to be sure:

  • How to call particular method in action class from Portlets StrutsContent

    I am developing a web application which uses weblogic portlets and struts. This is what I have for now in the .portlet file.
    +<netuix:strutsContent action="getStudentList" module = "people/students"+
    refreshAction = "getStudentList" reqestAttrpersistence="none"/>
    I want it to change something like this:
    +<netuix:strutsContent action="getStudentList.do?method=allGrads" module = "people/students"+
    refreshAction = "getStudentLis.do?method=allGrads" reqestAttrpersistence="none"/>
    But this is not working. So how can I achieve something like that?
    Thanks
    Edited by: user13634949 on Jun 23, 2011 1:22 PM
    Edited by: user13634949 on Jun 23, 2011 1:22 PM

    No, i dont know how to call other than main methods from other classes
    And one more doubt i have i.e. Like in C can we see the execution of the program like how the code is being compiled step by step and how the value of the variable changes from step to step like(Add Watch).........

  • Servlet with dopost and main() methods

    I have pre-existing servlet code for maintenance purposes.
    It received a request by POST action from the client. This class also has main() method where class is instasiated and processed.
    Can anyone help me understand reason for having dopost and main() methods.
    TIA

    Before the days of JUnit developers would write a main method for a class as a way to test the class. Some older developers still do it. I know because I am dealing with the problem now.

  • Passing arguments to main method from a shell script

    The main method of my class accept more than 10 arguments so my shell script execute this class like this :
    java -cp xxxx mypackage.MyClass $*
    The problem is that some of those arguments allows spaces. When i execute this script like this :
    myscript.sh firstparam secondparam ...... tenthparm "eleventh param"
    or
    myscript.sh firstparam secondparam ...... tenthparm 'eleventh param'
    the java class consider that my eleventh param is 'eleven' and not 'eleventh param'
    The same class works well with an equivalent dos script.

    I had this problem once also, and I found there are several ways to fix it. One way is to replace all of the spaces in the arguments with _ characters, or you can concantate all of the arguments into one String at runtime (with spaces in between) and use code to separate the arguments out. With the quotation marks, this code would be simple. If the spaces in one argument are used to divide different parts of the argument, just make them spearate arguments.

  • Multiple main methods

    Can we have more than one main method in a class/in various classes?if not why? and if yes then is that possible to call main() method with some other object nam,,,,say for eg...j.main()?
    As I'm new to java if anybody can explain me using an example as applicable it would be a better understanding for me over the concept

    It's not necessary to have main in a class and it's also ok to have main in multiple classes.
    You can also have multiple methods named 'main' in a class ( confusing, but legal ) as long as they follow the rules for overloading.
    The entry point for a Java program has to be a method with the signature
    public static void main ( String [] args )If you don't have this, you can't run that class. If you do, you can.
    The method has to be public because it will be called by the OS. It also has to be static since the object will not be instantiated till later; the runtime environment needs to be able to call the method without having to instantiate an object. The String array as parameter is to allow passing of command line arguments to the class.
    EDIT: A little slow there.
    Edited by: nogoodatcoding on Oct 16, 2007 5:48 PM

  • JFrame inside main method

    I have a JFrame object called mainJFrame constructed inside the main method in a program which I have made. Later I would need to use the getWidth() on the mainJFrame object in another class. Don't know how to design this. I tried to make the mainJFrame a public constant inside the class which contain the main method.
    This is a school project and in the specifications it is written that all the object have to be private, so I guess I cant make it as an public constant which I have made it. my program is already working but I just need to have it running with the mainJFrame object reference not as a public reference.
    /Thanks in regard from a future Java programmer

    bold This is where I call the JFrame....(it is called just frame and not mainFrame which I wrote earlier) bold
    public class BouncePanel2 extends JPanel implements ActionListener {
         public void actionPerformed(ActionEvent e) {
              if(e.getSource() == timer) {
    setSize( ( bold BounceMain.frame.getWidth() bold /2)-4, (BounceMain.frame.getHeight() )-32);
    bold this is a working version of the class containing the main method bold
    public class BounceMain {
         bold public static final JFrame frame= new JFrame("Bounce slutversion"); bold
         public static void main(String[] args) {
              MainPanel panel = new MainPanel();
              frame.getContentPane().add(panel );
              frame.setSize(604,632);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              frame.setVisible(true);     
    bold an this is another working verison bold
    public class BounceMain {
         static public JFrame frame;
         public static void main(String[] args) {
              bold frame = new JFrame("Bounce slutversion"); bold
              MainPanel panel = new MainPanel();
              frame.getContentPane().add(panel );
              frame.setSize(604,632);
              frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
              frame.pack();
              frame.setVisible(true);     
    but both these versions contains a public JFrame reference
    I will look at your reply which you wrote but I just wanted to write this if case it would clarify anything...

  • About main() method

    can any body tell me where the main method is declear.....is main () method written in java compiler or in a package.....????

    bubun_java wrote:
    main is a starting position of a program is written in jvmWhat?
    ..does it write..??More what?
    Look I don't know what you're on about with the jvm business. Main is a method you write. Then when you invoke java it looks for a main method in the class you provide. Or if this is a jar with a manifest it tries to find a main method in the classname provided in the manifest.
    There's no magic here. No secret methods or magic classes here.
    If you have a specific doubt then please give some example code or an example situation that explains it. Saying things like "main is a starting position of a program is written in jvm" is not unhelpful to use in clearing your doubt because it doesn't make any sense.

Maybe you are looking for

  • Skillbuilders Modal Page (2.0.0) - iframe height restricted to 150px?

    Hi, I installed the Skillbuilders Modal Page v 2.0.0 this afternoon into our Apex 4.1.1 instance and have most functions working as expected. I have the modal window popping up when a link column in an interactive report is clicked. However, iframe n

  • FI validation on IDoc sent by XI is failing

    Hi All,          We have requirment that when IDoc is posted sent by Xi, a certain validation is run on the SAP end which checks that if PRPS-SCOPE = 'INVST' and COBK-USNAM != 'POWERPLANT', then only the IDoc should not get posted. and we are sending

  • Purchasing apps

    It feels like something has changed recently in iTunes with iPhone.  I wonder if it has something to do with the pending release of iOS 5 and / or iCloud.  Having had the iPhone for a while now, I'm kind of saturated in the apps I want (I have about

  • Throw out old mini DV cassettes?

    Over the years I have created a number of movies and subsequently saved these back onto the tape and then created a DVD in iDVD. I now have 22 cassettes and 22 DVDs. The original footage has been deleted. I presume that if I wanted another copy of on

  • User Exist for Sales Order

    Hi All, When the Route changes in the Sales Order, I want to add the Forwarding agent to the Partfner function atutomatically. So I written the code in the User-Exist "userexit_check_vbap" with the following logic. IF sales order is creating with VA0