Can we call a static method without mentioning the class name

public class Stuff {
     public static final int MY_CONSTANT = 5;
     public static int doStuff(int x){ return (x++)*x;}
import xcom.Stuff.*;
import java.lang.System.out;
class User {
   public static void main(String[] args){
   new User().go();
   void go(){out.println(doStuff(MY_CONSTANT));}
}Will the above code compile?
can be call a static method without mentioning the class name?

Yes, why do it simply?
pksingh79 wrote:
call a static method without mentioning the class name?For a given value of   "without mentioning the class name".
    public static Object invokeStaticMethod(String className, String methodName, Object[] args) throws Exception {
        Class<?>[] types = new Class<?>[args.length];
        for(int i=0;i<args.length;++i) types[i] = args==null?Object.class:args[i].getClass();
return Class.forName(className).getDeclaredMethod(methodName,types).invoke(null,args);

Similar Messages

  • Calling a static method when starting the server???

    Hi,
    i wanted to call a static method of a java class,
    whenever i start the server.
    plz give some input on this.
    thanks and regards
    siva

    Siva -- Can you be more specific as to the problem you are trying to solve (why do you want to call this static method,.)
    The reason I ask is that there are some ways to make this happen but they may have limitations
    that won't work for you. For instance, you can create a servlet that calls your class' static method and then make the
    servlet be loaded on startup.
    Thanks -- Jeff

  • How to call a instance method without creating the instance of a class

    class ...EXCH_PRD_VERT_NN_MODEL definition
    public section.
      methods CONSTRUCTOR
        importing
          value(I_ALV_RECORDS) type ..../EXCH_VBEL_TT_ALV
          value(I_HEADER) type EDIDC .
      methods QUERY
        importing
          I_IDEX type FLAG
          i_..........
        returning
          value(R_RESULTS) type .../EXCH_VBEL_TT_ALV .
    Both methods are instance methods.
    But in my program i cannot created instance of the class unless i get the results from Query.
    I proposed that Query must be static, and once we get results we can create object of the class by pasing the result table which we get from method query.
    But i must not change the method Query to a static method.
    Is there any way out other than making method Query as static ?
    Regards.

    You can't execute any instance method without creating instance of the class.
    In your scenario, if you don't want to process your method of your class, you can check the instance has been created or not.
    Like:
    IF IT_QUERY IS NOT INITIAL.
      CRATE OBJECT O_QUERY EXPORTING......
    ENDIF.
    IF O_QUERY IS NOT INITIAL.
    CALL METHOD O_QUERY->QUERY EXPORTING....
    ENDIF.
    Regards,
    Naimesh Patel

  • How can i call a main method  from a different class???

    Plzz help...
    i have 2 classes.., T1 and T2.. Tt2 is a class with a main method....i want to call the main method in T2......fromT1..is it possibl..plz help

    T2.main(args);

  • Call a static method via RFC

    Hello people,
    How can I call a static method via RFC?
    I am in SRM and would like to call a static method defined in ECC.
    What is necessary to be configured in this class/method?
    Thanks!!!

    You may need to write RFC in ECC and call it from SRM
    and write following code in RFC for call a static method
    reference_obj=>method_name
    EXPORTING

  • How to call a static method in a class if I have just the object?

    Hello. I have an abstract class A where I have a static method blah(). I have 2 classes that extend class A called B and C. In both classes I override method blah(). I have an array with objects of type B and C.
    For every instance object of the array, I'm trying to call the static method in the corresponding class. For objects of type B I want to call blah() method in B class and for objects of type C I want to call blah() method in C class. I know it's possible to call a static method with the name of the object, too, but for some reason (?) it calls blah() method in class A if I try this.
    So my question is: how do I code this? I guess I need to cast to the class name and then call the method with the class name, but I couldn't do it. I tried to use getClass() method to get the class name and it works, but I didn't know what to do from here...
    So any help would be appreciated. Thank you.

    As somebody already said, to get the behavior you
    want, make the methods non-static.You all asked me why I need that method to be
    static... I'm not surprised to hear this question
    because I asked all my friends before posting here,
    and all of them asked me this... It's because some
    complicated reasons, I doubt it.
    the application I'm writing is
    quite big...Irrelevant.
    Umm... So what you're saying is there is no way to do
    this with that method being static? The behavior you describe cannot be obtained with only static methods in Java. You'd have to explicitly determine the class and then explicitly call the correct class' method.

  • How to call a static method from an event handler

    Hi,
       I'm trying to call a static method of class I designed.  But I don't know how to do it.  This method will be called from an event handler of a web dynpro for Abap application.
    Can somebody help me?
    Thx in advance.
    Hamza.

    To clearly specify the problem.
    I have a big part code that I use many times in my applications. So I decided to put it in a static method to reuse the code.  but my method calls functions module of HR module.  but just after the declaration ( at the first line of the call function) it thows an exception.  So I can't call my method.

  • Can i call non -abstract method in abstract class into a derived class?

    Hi all,
    Is it possible in java to call a non-abstract method in a abstact class from a class derived from it or this is not possible in java.
    The following example will explain this Ques. in detail.
    abstract class A
    void amethod()
    System.out.println(" I am in Base Class");
    public class B extends A
    void amethod()
    System.out.println(" I am in Derived Class");
    public static void main (String args[])
    // How i code this part to call a method amathod() which will print "I am in Base Class
    }

    Ok, if you want to call a non-static method from a
    static method, then you have to provide an object. In
    this case it does not matter whether the method is in
    an abstract base class or whatever. You simply cannot
    (in any object oriented language, including C++ and
    JAVA) call a nonstatic method without providing an
    object, on which you will call the method.
    To my solution with reflection: It also only works,
    if you have an object. And: if you use
    getDeclaredMethod, then invoke should not call B's
    method, but A's. if you would use getMethod, then the
    Method object returned would reflect to B's method.
    The process of resolving overloaded methods is
    performed during the getMethod call, not during the
    invoke (at least AFAIK, please tell me, if I'm wrong).You are wrong....
    class A {
        public void dummy() {
             System.out.println("Dymmy in A");
    class B extends A {
         public void dummy() {
              System.out.println("Dymmy in B");
         public static void main(String[] args) throws Exception {
              A tmp = new B();
              Class clazz = A.class;
              Method method = clazz.getDeclaredMethod("dummy", null);
              method.invoke(tmp, null);
    }Prints:
    Dymmy in B
    /Kaj

  • Can only see 4 static methods out of 14

    I have a single public class containing about 14 public static methods and some inner clasess.
    When using the JDeveloper Ver3.1 build 681, to deploy these java methods as RDBMS stored procs, only 4 of the methods & inner classes are visible to publish.
    The methods available to publish are main(), another method which calls most of the other non-available methods and 2 completely independent methods.
    I need a clarification on my understaning.
    If a method A calls another method B in the same class but B is not published as a stored proc.
    When I invoke A, RDBMS sees a java source stored procedure and JSERVER kicks in. I presume that JSERVER can see all the methods declared in the class as I see in the *.java file.
    When you deploy, what is taken across from the *.java file apart from connection properties details.
    Any help/pointers will be appreciated. Thanx in advance.

    I have a single public class containing about 14 public static methods and some inner clasess.
    When using the JDeveloper Ver3.1 build 681, to deploy these java methods as RDBMS stored procs, only 4 of the methods & inner classes are visible to publish.
    The methods available to publish are main(), another method which calls most of the other non-available methods and 2 completely independent methods.
    I need a clarification on my understaning.
    If a method A calls another method B in the same class but B is not published as a stored proc.
    When I invoke A, RDBMS sees a java source stored procedure and JSERVER kicks in. I presume that JSERVER can see all the methods declared in the class as I see in the *.java file.
    When you deploy, what is taken across from the *.java file apart from connection properties details.
    Any help/pointers will be appreciated. Thanx in advance.

  • Can we call a BO method directly

    Hi all,
    Can we call a BO method  explicitly in a program .
    e.g. can i  call the method approve of the  FORMABSENC BO.
    Regards,
    Sateesh.

    Of course you can. You just need to include the macros include so you can create objects and call the methods. If you are not familiar with the different macros, just browse through <CNTN01> to find what you need. There is documentation in the code.
    You will probably need at least the following four macros
    SWC_CREATE_OBJECT to create an object from its type and key.
    SWC_SET_ELEMENT one or more times to fill the container with appropriate parameters.
    SWC_CALL_METHOD to call the method.
    SWC_GET_ELEMENT one or more times to retrieve returned parameters.
    Note that SWC_SET_ELEMENT does not work for table parametes, use SWC_SET_TABLE instead.

  • How to get the class name  static method which exists in the parent class

    Hi,
    How to know the name of the class or reference to instance of the class with in the main/static method
    in the below example
    class AbstA
    public static void main(String[] args)
    System.out.println(getXXClass().getName());
    public class A extends AbstA
    public class B extends AbstA
    on compile all the class and run of
    java A
    should print A as the name
    java B
    should print B as the name
    Are there any suggestions to know the class name in the static method, which is in the parent class.
    Regards,
    Raja Nagendra Kumar

    Well, there's a hack you can use, but if you think you need it,Could you let me the hack solution for this..
    you probably have a design flaw and/or a misunderstanding about how to use Java.)May be, but my needs seems to be very genuine..of not repeat the main method contents in every inherited class..
    The need we have is this
    I have the test cases inheriting from common base class.
    When the main method of the test class is run, it is supposed to find all other test cases, which belong to same package and subpackages and create a entire suite and run the entire suite.
    In the above need of the logic we wrote in the main method could handle any class provided it knows what is the child class from which this main is called.
    I applicate your inputs on a better way to design without replicating the code..
    In my view getClass() should have been static as the instance it returns is one for all its instances of that class.
    I know there are complications the way compiler handles static vars and methods.. May be there is a need for OO principals to advance..
    Regards,
    Raja Nagendra Kumar
    Edited by: rajanag on Jul 26, 2009 6:03 PM

  • In terms of scalabilty, will static method in normal java class better than

    You only have one copy of the static method in memory, but you have a few copy of the stateless session bean, plus the overhead of those session bean.So isn't static method of normal java class more scalable than stateless session bean.

    Then you have to take care of transaction management, connection pooling, etc other ejb services all by your self.
    In fact stateless session beans acts more like a static class. They do not get destroyed when remove() method is called on client stub. Similarly they are not always gets created whenever someone calls create() method on home interface!! (Again it's specific to appserver implementation)
    Thx.

  • Call of the method START_ROUTINE of the class LCL_TRANSFORM failed; wrong type for parameter SOURCE_PACKAGE

    Hi,
    My DTP load failed due to following error.
    Call of the method START_ROUTINE of the class LCL_TRANSFORM failed; wrong type for parameter SOURCE_PACKAGE
    I don't think anything wrong with the following code as data loads successfully every day. Can any one check?  What could be the issue?
    METHOD start_routine.
    *=== Segments ===
         FIELD-SYMBOLS:
           <SOURCE_FIELDS>    TYPE _ty_s_SC_1.
         DATA:
           MONITOR_REC     TYPE rstmonitor.
    *$*$ begin of routine - insert your code only below this line        *-*
    *   Fail safe which replaces DTP selection, just in case
         DELETE SOURCE_PACKAGE WHERE version EQ 'A'.
    *   Fill lookup table for the ISPG
         SELECT p~comp_code m~mat_plant m~/bic/zi_cpispg
           INTO TABLE tb_lookup
         FROM /bi0/pplant AS p INNER JOIN /bi0/pmat_plant AS m ON
           p~objvers EQ m~objvers AND
           p~plant   EQ m~plant
         FOR ALL ENTRIES IN SOURCE_PACKAGE
         WHERE p~objvers   EQ 'A' AND
               p~comp_code EQ SOURCE_PACKAGE-comp_code AND
               m~mat_plant EQ SOURCE_PACKAGE-material.
         SORT tb_lookup BY comp_code material.
    *$*$ end of routine - insert your code only before this line         *-*
       ENDMETHOD.     

    Hi,
    Compare the data types of the fields in your table tb_lookup with the data types in your datasource. Most probably there is an inconsistency with that. One thing that I realize is that
    I use the select statement in the format select ... from ... into... Maybe you need to change places of that, but I am not sure. You may put a break point and debug it with simulation.
    Hope it gives an idea.
    Yasemin...

  • This static method cannot hide the instance method from...

    What means the error message "This static method cannot hide the instance method from Referee"?
    Referee.java is a interface. I implemented it in the class RefereeMyName.java and made a method in that class to be static. Thats why I received that error message.
    But can't I use static methods when I have implemented a interface in that class? I want to have a Player.java class which I want to access RefereeMyName.getTarget(). I cannot use a instance instead because I wouldn't receive a valid return value then (RefereeMyName is the client class).
    What is the solution?

    Hi,
    Well i do not think that you can do that b'cos that way you are not giving the same signature for the method as that exists in the interface. I do not know how other way it can be done but if something urgent for you then you can remove that method from that interface and only define in the class.
    Regards,
    Shishank

  • HT202213 Can multiple iPhones share one iTunes without syncing the phones together? We just got new iPhones and set up desperate apple id's, but don't know if we can both sync to our existing iTunes without linking our phones together (contacts, apps, etc

    Can multiple iPhones share one iTunes without syncing the phones together? We just got new iPhones and set up seperate apple id's, but don't know if we can both sync to our existing iTunes without linking our phones together (contacts, apps, etc) ????

    How to use multiple iPods, iPads, or iPhones with one computer

Maybe you are looking for

  • Error code 0xC004F074

    whenever I open my Notebook, PC setting page gets open everytime. reminds to activate windows. whenever I try to activate it shows error code: 0xC004F074 also I am unable to personalize my notebook. It reminds for activating windows. I have already u

  • Plugged in HDMI to TV, have sound problem!

    I just plugged in a HDMI cable from my PC to my TV, I did the thing in which i can slide the bars across each screen but I can only play sounds either from my TV or from my PC, what can i do so that I can hear both simultaneously?

  • Empty (ignore)

    1

  • ScrollPane Scrolling Problem

    I have add a big panel to my scrollpane but the scrollpane does not show the scrollbar.... so please help me..................

  • Authorisation missing

    Dear Gurus ,                    When i ran mbbs report in our ECC server , we will get the error shown below .                    You have no authorization for this transaction in plant TB01 Message no. M7120 Diagnosis According to the settings made