Dynamic programming accessing static method on classes
Hi
I need access to a static method on a class.
The class name is first decided at runtime.
How do I do that?
Regards,
Morten
Hello Morten
Here is a sample program for you:
*& Report ZUS_SDN_DYNAMIC_METHOD_CALL
REPORT zus_sdn_dynamic_method_call.
DATA: go_msglist TYPE REF TO if_reca_message_list.
DATA: gd_method TYPE tmdir-methodname,
gd_class TYPE seoclsname.
START-OF-SELECTION.
IF ( 1 = 2 ).
CALL METHOD cf_reca_message_list=>create
* EXPORTING
* id_object = 'RECA'
* id_subobject = 'MISC'
* id_extnumber =
RECEIVING
ro_instance = go_msglist.
go_msglist = cf_reca_message_list=>create( ).
ENDIF.
gd_class = 'CF_RECA_MESSAGE_LIST'.
gd_method = 'CREATE'.
CALL METHOD (gd_class)=>(gd_method)
RECEIVING
ro_instance = go_msglist.
IF ( go_msglist IS BOUND ).
WRITE: / 'Class is bound'.
ELSE.
WRITE: / 'Class is NOT bound'.
ENDIF.
END-OF-SELECTION.
Obviously, that is only half of the task. You need to make the method signature dynamically as well.
Regards
Uwe
Similar Messages
-
I am not able access static methods from startup class
I have a simple java class which queries the Database and holds some data. I am trying to access a static method of this class from the StartupClass and I am not able to do so. But I am able to access a static variable of the same class.
Can someone help me here.Well, welcome to the world of "hacked" phones. If your purchase was from the US, you obviously purchased an iPhone that is locked to a US carrier. The iPhone must have been jailbroken and unlocked via software to accommodate your foreign SIM card. Now that you have updated the phone, it is locked back to the US carrier. Only the carrier can authorize Apple to unlock the iPhone and none of the US carriers will unlock the iPhones. Best suggestion is to sell the phone and obtain a factory unlocked version directly from the Apple store.
To answer your other question, there is not a supported method to downgrade iOS. -
Static methods in class accessed by jsp/servlet
I wanted to conceptually know ...
If I have a class specifically doing some quering to database and giving results to jsp and servlet ... does this make sense? Static methods are ok ? Is it secure enough if I have all connection classes local to a method? can i make the connection global instead without compromising security ?
For example:
public class DatabaseUtility{
public static Sring getUsername(String employeeid)
Connection conn = ...........
Statement stmt = ........
rs executes query and gets a username and returns it...
public static Sring getAddress(String employeeid)
Connection conn = ...........
Statement stmt = ........
rs executes query and gets a address and returns it...
}can i make the connection global instead without compromising security ?As long as it was readonly, it should be secure. However as damiansutton said, you open yourself up to a resource bottleneck if you make the connection static, as everyone will be using the same connection.
Most often if you want one piece of information from this DatabaseUtility, you want more than one. Why get a database connection each time? You want to get a DB connection, milk all the info you want from it, and then give it back.
I think you might want to look at having a DataAccessObject: http://java.sun.com/blueprints/corej2eepatterns/Patterns/DataAccessObject.html
It is no longer static, but is much more efficient in that it only creates one database connection for all your queries on one request. So anywhere you want to use this, you create a new DatabaseUtility object, and then use it to your hearts content.
Cheers,
evnafets -
Static methods inside class in jsp can't make it work
Hello all i can't understand it why i can't declare the method as static method ?
public class Request{
Request(){}
private String paramValue;
public static String getRequestParam(HttpServletRequest request, String paramName, String defaultValue){
paramValue = request.getParameter(paramName);
return paramValue != null ? paramValue : defaultValue;
im geting this error :
The method getRequestParam cannot be declared static; static methods can only be declared in a static or top level type
can't i do inner class ?
or other sulotion for me not to make instences of class's but to use the class static methods?Hi, try this:
public class Request{
Request(){}
private static String paramValue;
public static String getRequestParam(HttpServletRequest request, String paramName, String defaultValue){
paramValue = request.getParameter(paramName);
return paramValue != null ? paramValue : defaultValue;
}it should work. the problem in you code is that you are trying to referance an non-static object with a static one!
Message was edited by:
Adelx -
I really need abstract static methods in abstract class
Hello all.. I have a problem,
I seem to really need abstract static methods.. but they are not supported.. but I think the JVM should implement them.. i just need them!
Or can someone else explain me how to do this without abstract static methods:
abstract class A {
abstract static Y getY();
static X getX() {
// this methods uses getY, for example:
y=getY();
return new X(y); // or whatever
class B extends A {
static Y getY() { return YofB; }
class C extends A {
static Y getY() { return YofC; }
// code that actually uses the classes above:
// these are static calls
B.getX();
A.getX();I know this wont compile. How should i do it to implement the same?Damn i posted this in the wrong thread.. anyways.
Yes offcourse i understand abstract and static
But i have a problem where the only solution is to use them both.
I think it is theoretically possible ot implement a JVM with support for abstract static methods.
In fact it is a design decision to not support abstract static methods.. thats why i am asking this question.. how could you implemented this otherwise?
There is an ugly soluition i think: using Aspect Oriented Programming with for example AspectJ.. but that solution is really ugly. So anyone has an OO solution? -
Can't get ClassLoader from static method.
I'm trying to get the ClassLoader within a static method getInstance().
public class PropertyManager {
public static PropertyManager getInstance(String fileName) {
//The following line returns null
ClassLoader cl = (new Object()).getClass().getClassLoader();
URL url = cl.getResource(fileName);
etc...
However if getInstance gets called from e.g. a statless session bean, a null
is returned instead of a valid ClassLoader.
The same code executed from a standalone java program (e.g. from
main(String[] args)) returns a valid ClassLoader
Why does getClassLoader behave differently in WebLogic than in a standalone
java program?
Thanks for you help.
BernieSorry for the confusion. I wrote a couple of more test programs and was able
to confirm that WL behaves exacltly the same as java does. For some reason
my intial tests were messed up...
getClassLoader() executed from a static method always returns null (in WL as
well as in a java standalone program).
In this case ClassLoader.getSystemClassLoader() will return a valid class
loader.
Thanks for your help.
Bernie
"Rob Woollen" <[email protected]> wrote in message
news:[email protected]..
Bernhard Lenz wrote:
I tried that as well, but getClassLoader() still returns null when
executed
in WL.
I'm still not clear how getClassLoader() gets affected by running withinWL.
>>
>
Very odd. Where is this class located? in the server's classpath, in ajar or
war file? Does getClass().getClassLoader work from a non-static method inthis
class?
-- Rob
"Rob Woollen" <[email protected]> wrote in message
news:[email protected]..
You want PropertyManager.class.getClassLoader()
-- Rob
Bernhard Lenz wrote:
I'm trying to get the ClassLoader within a static method
getInstance().
>>>>
public class PropertyManager {
public static PropertyManager getInstance(String fileName) {
//The following line returns null
ClassLoader cl = (new Object()).getClass().getClassLoader();
URL url = cl.getResource(fileName);
etc...
However if getInstance gets called from e.g. a statless sessionbean, a
null
is returned instead of a valid ClassLoader.
The same code executed from a standalone java program (e.g. from
main(String[] args)) returns a valid ClassLoader
Why does getClassLoader behave differently in WebLogic than in astandalone
java program?
Thanks for you help.
Bernie -
Dynamic Programming - RTTC - Appending lines
I give up! Maybe someone else can help me with this Dynamic Programming problem.
I am using RTTC to create an itab. Now I want to append lines and for some reason can't seem to crack the syntax I need.
My latest attempt looks like this...
FIELD-SYMBOLS: <table> TYPE ANY TABLE,
<row> TYPE ANY.
lo_sdescr = cl_abap_structdescr=>create( lt_components ).
lo_tdescr = cl_abap_tabledescr=>create( lo_sdescr ).
CREATE DATA lr_alloc->alloc_table TYPE HANDLE lo_tdescr.
CREATE DATA lr_struct TYPE HANDLE lo_rdescr.
ASSIGN lr_alloc->alloc_table->* TO <table>.
APPEND INITIAL LINE TO <table> ASSIGNING <row>.
The syntax check I get on the APPEND statement is "You cannot use explicit or implicit index operations on tables with type "ANY TABLE".
All the doco and examples I can find use a simple "SELECT * ... INTO CORRESPONDING FIELDS OF TABLE <table>" syntax which it not what I need to do.
Any help it appreciated.
Thanks
Graham RobboHello Graham
The solution is quite simple (at least to overcome your syntax error):
FIELD-SYMBOLS:
<gt_itab> TYPE STANDARD TABLE, " use STANDARD instead of ANY
<gs_struc> TYPE ANY.
*& Report Z_RTTI_CREATE_COMPLEX_ITAB
*& NOTE: revised version of ZUS_SDN_RTTI_CREATE_STRUCTURES
*& Thread: Dynamic Programming - RTTC - Appending lines
*& <a class="jive_macro jive_macro_thread" href="" __jive_macro_name="thread" modifiedtitle="true" __default_attr="980407"></a>
REPORT z_rtti_create_complex_itab.
TYPE-POOLS: abap.
DATA:
celltab TYPE lvc_t_styl. " becomes field of complex itab
DATA:
gd_tabnam TYPE string,
gd_tabfield TYPE string,
go_table TYPE REF TO cl_salv_table,
go_sdescr TYPE REF TO cl_abap_structdescr,
go_sdescr_new TYPE REF TO cl_abap_structdescr,
go_tdescr TYPE REF TO cl_abap_tabledescr,
gdo_data TYPE REF TO data,
gdo_handle TYPE REF TO data,
gs_component TYPE abap_compdescr,
gs_comp TYPE abap_componentdescr,
gt_components TYPE abap_component_tab.
FIELD-SYMBOLS:
<gt_itab> TYPE STANDARD TABLE,
<gs_struc> TYPE ANY.
PARAMETER:
p_tabnam TYPE tabname DEFAULT 'KNB1'.
START-OF-SELECTION.
" Describe structure
go_sdescr ?= cl_abap_structdescr=>describe_by_name( p_tabnam ).
gd_tabnam = go_sdescr->get_relative_name( ).
" Simulate dynamic addition of columns
LOOP AT go_sdescr->components INTO gs_component.
" Build fieldname
CONCATENATE gd_tabnam gs_component-name INTO gd_tabfield
SEPARATED BY '-'.
CLEAR: gs_comp.
gs_comp-type ?= cl_abap_datadescr=>describe_by_name( gd_tabfield ).
gs_comp-name = gs_component-name.
APPEND gs_comp TO gt_components.
ENDLOOP.
" Create instances of dynamic structure and dynamic internal table
go_sdescr_new = cl_abap_structdescr=>create( gt_components ).
go_tdescr = cl_abap_tabledescr=>create( go_sdescr_new ).
" Create data refence followed by table creation
CREATE DATA gdo_handle TYPE HANDLE go_sdescr_new.
ASSIGN gdo_handle->* TO <gs_struc>.
CREATE DATA gdo_handle TYPE HANDLE go_tdescr. " !!!
ASSIGN gdo_handle->* TO <gt_itab>.
APPEND INITIAL LINE TO <gt_itab> ASSIGNING <gs_struc>.
TRY.
CALL METHOD cl_salv_table=>factory
IMPORTING
r_salv_table = go_table
CHANGING
t_table = <gt_itab>.
go_table->display( ).
CATCH cx_salv_msg .
ENDTRY.
END-OF-SELECTION.
Regards
Uwe -
The purpose of static methods ....?
In lay mans terms can anyone tell me WHY we have static methods ?
In lay mans terms can anyone tell me WHY we have
static methods ?Static methods become class level methods and are no longer confined within the objects of the class.
Static methods typically provide information about the class as a whole, such as the number of created objects etcetera. Another use is as an object factory. Instead of doing new all over the program you have a static create method you call to get new objects of the class. -
Static methods are class spacific
By mistake First message was posted by me Sorry........
Static methods are class spacific these methods are not overridable by sub class,
But if i write
class Super{
public static void printFunction(){
System.out.println("In Super Class Method");
public class SubClass extends Super{
private static void printFunction(){
System.out.println("In Super Class Method");
}compiler gives me error
c:>javac SubClass.java
SubClass.java:12: printFunction() in SubClass cannot override printFunction() in
Super; attempting to assign weaker access privileges; was public
private static void printFunction(){
1] Why he is trying to override the printFunction() Which is static?
2] if it is not overriding then why this error comes?
Edited by: rahul_p on Apr 28, 2008 7:45 AM -
Static method vs synchronisation
Hi,
A fundamental query.
If i am declaring a method as static, can i also declare it as sunchronized? i.e. can i have something like
public static synchronized sss(){
xxx
If a method is static, there is only one instance in the memory. Correspondingly, only one client can execute it at a time. in which case it is automatically synchronized.
Pl correct my reasoning.
When do we have the requirement of having a static method, or probably a singleton??
Thanks,
Prashant GuptaHi,
A fundamental query.
If i am declaring a method as static, can i also declare it as sunchronized? i.e. can
i have something like
public static synchronized sss(){
xxx
}?Yes, static methods can be synchronized which means that when any thread is running within a synchronized static method of class C, no other thread may enter a static synchronized method of that class. In other words, the monitor acquired by a static synchronized method is the Class itself while the monitor acquired by a non-static synchronized method is the object refered to by 'this'.
If a method is static, there is only one instance in the memory. Correspondingly,
only one client can execute it at a time. in which case it is automatically synchronized.There is (in most circumstances) a single copy of the definition (bytecodes representing all methods - static or not - and static fields) of of a class. However, any number of threads may be executing within any section of code unles that code is guarded by a synchronized block or within a synchronized method. In that case, no two threads can enter (well, actually be runnable within - Object.wait() has special implications) a synchronized block monitored by the same object.
When do we have the requirement of having a static method, or
probably a singleton??Not sure what you are asking here. You define static methods when there is behavior you want to implement that does not need to have access to instance (non-static) fields or methods and which would never need to be overridden (for purposes of polymorphism) by derived classes.
Wen needing a singleton, you have a choice of defining a "true singleton" (i.e. a class with a private constructor, some sort of static getInstance() method, with the bulk of its methods anf fields are non-static) or defining a class whose methods are all static and whose fields are also all static. Singletons. Use the one that fits you needs the best.
Chuck -
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. -
Using static methods to instantiate an object
Hi,
I have a class A which has a static method which instantiates it. If I were to instantiate the class A twice with it's static method within class B then you would think class B would have two instances of class A, right?
Example:
class A
static int i = 0;
public A(int _i)
i = _i;
public static A newA(int _i)
return new A(_i);
class B
public B()
A.newA(5);
A.newA(3);
System.out.println(A.i);
public static void main(String[] args)
B b = new B();
}Is there really two instances of class A or just one? I think it would be the same one because if you check the int i it would be 3 and not 5. Maybe static instantiated objects have the same reference within the JVM as long as you don't reference them yourself?
-BruceIt
seems like you are the only one having trouble
understanding the question.Well I was only trying to help. If you don't want any help, its up to you, but if that's the case then I fail to see why you bothered posting in the first place...?
Allow me to point out the confusing points in your posts:
you would think class B would have two
instances of class A, right?The above makes no sense. What do you mean by one class "having" instances of another class? Perhaps you mean that class B holds two static references to instances of Class A, but in your code that is not the case. Can you explain what you mean by this?
Is there really two instances of class
A or just one?You have created two instances.
>I think it would be the same one because if you
check the int i it would be 3 and not 5.
I fail to see what that has to do with the number of instances that have been created...?
Maybe static instantiated objects have the
same reference within the JVM as long as you
don't reference them yourself????? What is a "static instantiated object"? If you mean that it was created via some call to a static method, then how is it different to creating an object inside the main method, or any other method that was called by main? (i.e. any method)
What do you mean by "the same reference within the JVM"? The same as what?
What happened to the first call "A.newA(5)"?
I think it was overidden by the second call
"A.newA(3)".As i already asked, what do you mean by this?
If you change the line for the constructor
in A to i = i * i you will get "9" as the
output, if you delete the call "A.newA(3)"
you will get "25" as the output. Yes. What part of this is cauing a problem? -
I have two questions regarding campaign
1)I have configured campaign for update events of content.
which will call static method of one class.but when any update occur in repository,exception comes for static method's class not found.
2)How to avoid stop date in campaign.so that it will run always.
Can anybody help me on the sameSince the campaigns run in the enterprise app classloader, anything called by the campaign needs to run in this classloader as well (APP-INF/lib). You could perhaps promote the code you want to call from the campaign from the web-app classloader to the enterprise-app classloader if you want it to be called.
Code in the enterprise-app classloader (APP-INF/...) can be called by code in the web-app classloader (WEB-INF/...) but not vice-versa. So it shouldn't be an issue to move the code to the ent-app classloader, and it can then be called by both the campaign as well as existing web-app code.
-Steve -
Java cognoscenti,
Anyone know why I can't declare a method as static in an interface, but I can in an abstract class. Surely, semantically it's the same - defering the implementation of a static method, not an abstract class and an interface. By the way, I'm using JDK 1.2.2 if that makes a differenceNo, it's not the same. You are not "defering the
implementation of a static method" because static
methods are never polymorphic.
Static methods cannot be abstract for this reason, and
only abstract methods are allowed in interfaces.I didnt't know that. I thought that - because you can override static methods and you can call them onto an actual instance the method binding would be done at run-time, not at compile-time. So I was trying to prove you wrong, but you are correct !
A short summary of what I've learnt so far:
- interfaces cannot contain static methods
- abstract classes cannot contain abstract static methods
- static methods can be overriden, but they are not polymorphic; this means that the compiler decides which method to call at compile-time.
/* output
SuperClass.someMethod()
SubClass.someMethod()
SuperClass.someMethod()
SuperClass.someMethod()
SubClass.someMethod()
SubClass.someMethod()
SuperClass.someMethod()
public class Test {
public final static void main(String[] args) {
// output: SuperClass.someMethod()
new SuperClass().someMethod();
// output: SubClass.someMethod()
new SubClass().someMethod();
// output: 2x SuperClass.someMethod()
SuperClass someClass1 = new SubClass();
someClass1.someMethod();
showSuper(someClass1);
// output: 2x SubClass.someMethod()
SubClass someClass2 = new SubClass();
someClass2.someMethod();
showSub(someClass2);
showSuper(someClass2); // SuperClass.someMethod()
public final static void showSuper(SuperClass c) {
c.someMethod();
public final static void showSub(SubClass c) {
c.someMethod();
class SuperClass {
public static void someMethod() {
System.out.println("SuperClass.someMethod()");
class SubClass extends SuperClass {
public static void someMethod() {
System.out.println("SubClass.someMethod()"); -
Static method or not static method
I have a simple class, ClassA, with no shared class variables, and has only one static method only:
class ClassA {
public static void doIO (args) {
try {
//very time consuming I/O task
catach (Exceptions e) {
//try to resolve the problem
This doIO method will be called simultaneously from different objects.
My questions are:
1) Would this static method approach cause any robustness, efficiency problem? Or, should I change the method into a non-static method, and create a new instance of ClassA every time?
2) If there are 20 threads call ClassA.doIO(args) at the same time, would the method be executed in parallel or queued?
Many thanks.
java9394Using a static method makes this implementation as efficient as possible. Since the class has no instance variables, there is no benefit, to either making it non-static, or creating multiple instances. If you have 20 threads calling it, they will run concurrently. So, if you do not like this behavior, the method can be declared both static, and synchronized.
Maybe you are looking for
-
How to find out the total, subtotal in alv report
hi dears, how to find out the total, subtotal in alv report? pls tell me logic , i will be waiting for eply regards eswar
-
Acrobat Forms Question - Secure Form Handling
I'll preface this post by saying I'm not a designer nor have I ever designed an Acrobat form. If the following is possible, I'll probably have to find a developer to do it for me. I have a small business that needs to receive and pass around by emai
-
I have a Macbook Pro (OS 10.4.7) and an Apple G4 running OS 9.2. Both are connected wirelessly to the Internet through an Apple Airport Express base station. The G4 has its own set of accessories and equipment, including a printer, flatbed scanner, f
-
Hello, I want to display a form from within BSP when a user hits a button. Allow me to explain : I've got a BSP page written in HTML and/or HTMLB combined with ABAP. There's a button on the form. In the OnInputProcessing, a custom Function Module
-
I purchased a prepaid 12 month Photoshop card. The system says my card is not activated
I purchased a 12 month Photoshop CC from McShark in Vienna. I have tried a number of times to enter the pass code on the back of the card, but the system will not accept my card but says it is not activated. As a result I am not able to download Phot