Can users call main method??

class MainDemo{
public static void main(String args[]){
System.out.println("First main method");
MainDemo.main("hello");
}Hi all,
Why is the above code not compiling??

By adding few lines right after the System.out.println("First main method");.
class MainDemo
     public static void main(String args[])
          System.out.println("First main method");
          System.out.println("No.of args : " + args.length);
          if (args.length > 0)
               System.out.println("Passed in : " + args[0]);
          System.out.println();
    static
         String args[] = {"hello"};
         MainDemo.main(args);
}And the output gives :-
First main method
No.of args : 1
Passed in : hello
First main method
No.of args : 0
Press any key to continue...
Why is it JVM executes the main() method in the static block instead of the calling the main() method (without argument) directly ?
Edited by: TKH on Nov 14, 2007 1:08 AM

Similar Messages

  • 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).........

  • Can we call a method in stateless session bean ?

    Can we call a method in stateless session bean in onMessage method?

    Hi,
    The purpose of service locator is to abstract the lookup of the server side services like ejb and DSNs. It should be a normal java class but as such there is no such restriction. You can have a look at the service locator patter at the j2ee blue prints.
    /Ashwani

  • Can we call a method from a adapter class?

    Hi
    I have added KeyAdapter class to a textfield now i want to call a method in the outer class from inside the adapter class. I tried but its showing some error,
    public class A
    A()
    TF1.addKeyListener(new KeyAdapter()
    public void keyReleased(KeyEvent e)
    A a=new A();
    ec.chkSalary();                    
    chkSalary()
    can i call the method like this?

    yeah.. i got it.. i just passed that KeyEvent object to the method
    but i did declare that as a char in that method like,
    chkSalary(e) // e is the KeyEvent reference
    and the method is
    chk(char e);
    thats it.
    thank you yaar.

  • How can I call a Method in a Transformation Routine

    Hello Experts,
    How can I call a Method in a Transformation Routine ??
    THNXX

    Hi,
    http://www.sdn.sap.com/irj/scn/go/portal/prtroot/docs/library/uuid/6090a621-c170-2910-c1ab-d9203321ee19?quicklink=index&overridelayout=true
    It will be help full .
    Regards,

  • How can we call the method of used controller?

    Hi All,
       i created two WDA Applications.( like YWDA1,YWDA2 ) . i am using the component WDA2 in WDA 1.and displaying the one view of WDA2 as popup window in WDA1 on action of one of the input element in the view of WDA1 by using the method l_window_manager->create_window_for_cmp_usage
    I have a button on the view of WDA2 which has appear in the popup window...how can i call the method which has binded to that button....and where should i code that...and i need to assign selected value in the popup window to input elemetn of view  WDA1
    Please help me to resolve this....
    Regards,
    Ravi

    You can not directly call view's event handler from other component.
    create a method in component controller of the second component and in the button click call the component controller method. ( also make the method as interface so that you can call it from other components )
    Now, you can call the interfacecontroller's method
    DATA: l_ref_INTERFACECONTROLLER TYPE REF TO ZIWCI__VSTX_REBATE_REQ_WD .
      l_ref_INTERFACECONTROLLER =   wd_This->wd_CpIfc_<comp usage name>( ).
      l_ref_INTERFACECONTROLLER->Save_Rr(
        STATUS = '01'                       " Zvstxrrstatus
    save_rr is the method of second component controller

  • How can I call a method for all instances of a certain class?

    Hi,
    I have created a snake java applet, which you can play here [http://users.telenet.be/javagames/|http://users.telenet.be/javagames/].
    But now I want to make this into a multi-player game (with two or more snakes etc..).
    In my main class you can find methods like these:
    snake.move();
    snake.drawSnake();Now if I just alter my main class and make a second instance of the class snake and change my methods to this,
    snake.move();
    snake2.move();
    snake.drawSnake();
    snake2.drawSnake();my game almost works perfectly, I'll have to make some minor changes though.
    But my question is, is there a better way of doing this. Instead of duplicating all the code, how can I make the methods immediately work for both snakes, instead of calling it twice?
    All suggestions are welcome!
    Thanks in advance.
    Lennart

    If there was something common to all snake instances then you could declare that variable static.
    Note: I don't want to confuse you here. A static variable can have a value apart from any instances at all. Instances can view static variables but a static method or inner class, for example, cannot view instance variables.
    More generally, I would think you would want to treat each snake as an automaton with its own behavior. There must be some class such as Game or Board or whatever that has, as suggested, either an array or collection of snakes. Iterate over each snake, ask it to perform some computation for the game and then draw itself.
    - Saish

  • 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...

  • 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

  • Can we call DLL method in jave code

    hi,
    can we call DLL file method in java Code or program.

    See
    http://forum.java.sun.com/thread.jsp?forum=52&thread=179765

  • How Can I Call a method in iView  ,which is in another iView

    I Have 2 iViews i want to call a method in Iview which is i another iView
    Thanks and Regards
    Prasad.Y

    Hi Prasad,
    first, welcom at SDN.
    Next, let us clarify some wording:
    A <i>PAR</i> is a <i>Portal ARchive</i>. It is a zip in a certain format. It contains a <i>portal application</i>.
    A <i>portal application</i> consists of <i>components</i> and/or <i>services</i>.
    An <i>iView</i> is somehow an "instance" of a <i>component</i>. It is created within the portal. There are <i>no iViews in a PAR</i> (like: "There are no objects in a JAR (but classes).").
    With this in mind, you know that the question does not work, for iViews don't have "methods".
    If you ask how you can call a method of a class belonging to a different component (but within the same portal app), it is no problem at all, each class has access to each other class within the same portal app (in fact, one has to differentiate between public and private part, but we don't want to go so far). To call use a class belonging to (the public part of) a different app, you will have to set the SharingReference within portalapp.xml.
    If that's what you're looking for - great.
    If not, ask again, but far more detailed.
    Hope it helps
    Detlev
    PS: Please consider to reward points for helpful answers. Thanks in advance.

  • Why can't call java method when I input  4 bytes character to the TextInput

    I am using Flex4 + Cairngorm2 + BlazeDS3.2 + Java + Spring
    Can't call java program when I input  4 bytes character in Flex.
    Example:
    Please help!

    Basic troubleshooting steps clearly outlined in the User Guide are restart, reset, restore from backup, restore as new device.
    Have you tried ALL the troubleshooting steps?  If you have and you're still having problems, then you'll need to bring your phone into Apple for evaluation and possible exchange.

  • Add a column and I can't call a method

    I'm trying to make a small portal app with IBM RAD 6.0 but I'm having a wierd problem calling a method in my page code. Any help would be great.
    Here's the cod I'm using, I've got a bunch of system.outs in it to help me track down what's happening as well as some commented code of some things I'm trying. Any why here goes:
    I start a data table in the jsp like this
    <h:dataTable value="#{pc_MaintainBasisFormView.memberBasisComponents}" var="results"
                             border="0" cellpadding="2" cellspacing="0"
                             columnClasses="columnClass1" headerClass="headerClass"
                             footerClass="footerClass" rowClasses="rowClass1"
                             styleClass="dataTable" id="table1">
    later I build these two columns
    <h:column id="startdatecolumn">
                                  <f:facet name="header">
                                       <h:outputText styleClass="outputText" value="Start Date" id="startdateheader" />
                                  </f:facet>
                                  <h:inputText styleClass="inputText" id="startdate" value="#{results.effectiveDates.effectiveStartDate}"></h:inputText>
                             </h:column>
                             <h:column id="column7">
                                  <f:facet name="header">
                                       <h:outputText styleClass="outputText" value="Remove" id="remove"></h:outputText>
                                  </f:facet>
                                  <h:commandLink styleClass="commandLink" id="removelink" action="#{pc_MaintainBasisFormView.removeBasisComponent}">
                                  <h:outputText id="text6" styleClass="outputText" value=">>"></h:outputText>
                                       <f:param name="krusty" value="#{results.id}"></f:param>
                                  </h:commandLink>
                             </h:column>
    so column startdate calls does this "results.effectiveDates.effectiveStartDate"
    so results has this method
    public EffectiveDates getEffectiveDates() {
              System.out.println("in getEffectiveDates " + effectiveDates);
              //effectiveDates = (effectiveDates == null) ? new EffectiveDates():effectiveDates;
              return effectiveDates;
    and EffectiveDates has this method
    public String getEffectiveStartDate() {
              System.out.println("in getEffectiveStartDate " + effectiveStartDate);
              effectiveStartDate = (effectiveStartDate == null) ? new Date() :effectiveStartDate;
              return "dfgdfG";
    (it's supposed to return a date but I've set it to a string just to try to get it working)
    and that all works fine.
    Now the second column calls "pc_MaintainBasisFormView.removeBasisComponent" with a parameter <f:param name="krusty" value="#{results.id}">
    and removeBasisComponent is defined as
         public String removeBasisComponent() {
              System.out.println("in removeBasisComponent");
              String id = getPortletRequest().getParameter("krusty");
              System.out.println("in krusty " + id);
              return "removeBasisComponent";
    so whats the probelm? When I click the link in the second column, the method that it is tied to is never called. BUT, when I don't have the first column, the link works as I expect it to.
    can anyone point me in the right direction to solve this problem?
    Thanks in advance
    JohnL

    Hi Ruhi
    I hope this answer doesn't come too late, for some reason the forum does not let me in w/ my username & password, and won't allow me to follow the "Forgot password" procedures.
    Anyway:
    I don't have the code but I'll try to explain the problem using the info I posted originally.
    my problems start here:
    <h:inputText styleClass="inputText" id="startdate" value="#{results.effectiveDates.effectiveStartDate}"></h:inputText>
    It generates an input box with the value results.effectiveDates.effectiveStartDate. What I didn't know was that when I tried to submit the form from the link the application was silently failing. By silently I mean there was no error in the log, nothing, not a clue as to what the problem was.
    The problem turned out to be pretty simple. When the form is generated with results.effectiveDates.effectiveStartDate
    it was essentially running results.getEffectiveDates().getEffectiveStartDate() which, from my example, returned a string. That's great, no problem.
    When the form is submitted it was trying to call results.getEffectiveDates().setEffectiveStartDate(), where the method setEffectiveStartDate must take a String argument, setEffectiveStartDate(String EffectiveDate). Remember that in my example I was returning a string to the from...so I needed to accept a string as an argument to the set method.
    I hope this helps.

  • How can I call a method of an object that is stored in an ArrayList?

    Hello,
    I am working on a simple game.
    I have my own collection class that can contain some Brick objects.
    That class looks like this:
    class MyCollection<Brick>
          ArrayList<Brick> list = new ArrayList<Brick>();
          public void add(Brick obj)
                list.add(obj);
         public int get(int i)
                return list.get(i);
         public int length()
               return list.size();
    }My Brick class is abstract and contains this method:
    public abstract int getXpos();That method is overridden by the subclass Brick_type1 (which, of course, extends Brick) and returns an int.
    In my main class I try to call the getXpos() method but I cannot call if for an unknown reason:
    MyCollection mc = new MyCollection<Brick>();
    Brick_type1 bt = new Brick_type1(10);
    mc.add(bt);
    for(int i = 0; i<mc.length(); i++)
                System.out.println(bc.get(i).getXpos());   // getXpos() does not exist, says the compilatorbc.get(i) should return a Brick that contains the getXpos() method, so why can´t I call it?

    Roxxor wrote:
    Sorry, it should be:
    public Brick get(int i)
    return list.get(i);
    }...so it actually returns a Brick, but I still cannot call the getXpos() method for some reason.Is this also just a typo?
    MyCollection mc = new MyCollection<Brick>();

Maybe you are looking for