cancel
Showing results for 
Search instead for 
Did you mean: 

Java Holder Usage ?

Former Member
0 Kudos

Dear All,

I am developing a Web service project where I am using the holder classes(StringHolder, LongHolder, etc) in my web service functions as parameters.

eg:

Void myFunction(StringHolder InOut, LongHolder outResult)

{

// assign some new values to Inout and outResult

}

My problem is that....after the client calls the function myFunction()

the values of Inout and outResult are the same as the initial

values and not the newly assigned values..

I am using SAP NetWeaver Developer Studio

Version: 7.0.09 , SAP netweaver 2004s

Any help?

Thanks,

Talimeren

Accepted Solutions (0)

Answers (1)

Answers (1)

Former Member
0 Kudos

Talimeren,

Can you please post more example code, i.e. the implementation of "myFunction"?

How are you setting the values of the holder classes? You cannot change the object reference of a parameter inside a function, you have to change the actual value inside the holder object.

Best regards,

Daniel

Former Member
0 Kudos

Thanks Daniel for your reply.

Actually I am simply setting the values and not trying to

change the object reference of the parameter.

Here's an example:

Void myFunction(StringHolder InOut, LongHolder outResult)

{

//do something

InOut.value="Updated value";

outResult.value=1L;

}

Former Member
0 Kudos

Hello Talimeren,

just looking at the code snippet you posted you should be seeing the updated values after you call myFunction. Maybe there's an error somewhere else in your code?

Former Member
0 Kudos

well..even if I change my function to

Void myFunction(StringHolder InOut, LongHolder outResult)
{
InOut.value="Updated value";
outResult.value=1L;
} 

without any code in the function myFunction except for the

assignment statements, the values are still the same initial values.

Thanks,

Talimeren

Former Member
0 Kudos

Just to make sure I am not going crazy I tried the following code:


public class Playground {

	public static void main(String[] args) {
		
		StringHolder sh1 = new StringHolder("Original Value");
		LongHolder lh1 = new LongHolder(2L);
		
		System.out.println(sh1.value + " " + lh1.value);
		
		Playground p = new Playground();
		p.myFunction(sh1, lh1);
		
		System.out.println(sh1.value + " " + lh1.value);
	}
	
	public void myFunction(StringHolder InOut, LongHolder outResult){
	        //	  do something
		InOut.value="Updated value";
		outResult.value=1L;}

}

This is the output I get:

<i>Original Value 2

Updated value 1</i>

When I posted that the problem is somewhere else in your code, I meant regarding the way you are calling the function or what you are doing immediately after or before it. You could try enabling Assert and placing an assert right after your call to myFunction to check the values there?

Former Member
0 Kudos

Hi Hermann Hans thanks for your reply

But I guess you haven't read my first post clearly.

I am developing a web service project

myFunction() is a web service function in that.

When I am calling for the client side I am not getting the

modified values of the holders. Between the web service functions

I am getting the modified values.

eg:

public class service
{
public void myFunction(StringHolder InOut, LongHolder outResult)
{
	        //	  do something
		InOut.value="Updated value";
		outResult.value=1;
}
void operation()
{
StringHolder sh1 = new StringHolder("Original Value");
LongHolder lh1 = new LongHolder(2);
myFunction(sh1, lh1);
//
//here I am getting the Updated value and Long value as 1;
}
}

but the problem is when the client calls the webservice. ..

I am not getting the updated values

thanks,

talimeren

Former Member
0 Kudos

Hopefully, I'm understanding what your issue is. I think it's the same thing that I was having trouble with recently. I was always under the assumption that Java did "pass by Reference" but was surprised that it does not.

If you want to get the modified value back you need to return a String from your method. If you need to return several values back from your method, I believe you're going to have to create a custom object that contains the properties you need, populate the object and then return that object.

Try this simple code (no comments please. I just threw it together) to see what I'm saying.

Hope that helps.

public class TestPassByRef {
	public static void main(String[] args) {
		String mainVal = "originalVal";
		TestPassByRef tp = new TestPassByRef();
		tp.changeIt1(mainVal);
		System.out.println("After changeIt1 val ='" + mainVal + "'");
		mainVal = "originalVal";
		mainVal = tp.changeIt2(mainVal);
		System.out.println("After changeIt2 val ='" + mainVal + "'");
	}
	private void changeIt1(String val) {
		System.out.println("changeIt1: coming in value is '" + val + "'");
		val = "newVal";
		return;
	}
	private String changeIt2(String val) {
		System.out.println("changeIt2: coming in value is '" + val + "'");
		val = "newVal";
		return val;
	}
}

Former Member
0 Kudos

Thanks David Z. Pantich for your reply.

But that is not what I want.

Actually I have used Holder before (java 1.5)

Right now I am using java 1.4 and dunno what is the problem

but I am not getting the assigned values.

thanks

Talimeren

Former Member
0 Kudos

Hello David,

I think we've already gotten to that point and we've agreed that his code should execute correctly assuming you are running it locally. However, the problem seems to be that while the code executes fine on the server (or when run locally), as soon as he runs it from a client which makes a call to the web service, the values are not changed when the function returns.

Maybe someone smarter than me could explain where the two objects are located that are being modified by myFunction? The server or the client? Possibly I am thinking about this the wrong way, but how is pass-by-reference (ok, ok, pass by value of the reference) implemented in this scenario?

Could it be the case that the client passes an address to the server (the value of the reference to the object sitting on the client) with which the server cannot do anything with?

Former Member
0 Kudos

My mistake. Sorry 'bout that.

Former Member
0 Kudos

Hello all,

Java always passes objects by references and primitives by value. However, if you modify the reference itself this will not be reflected in the calling method. Only changes in the object itself. This is the reason why David got the results in his test-code: With val = "newVal" you were changing the reference of the val object, but this wasn't reflected in the calling method.

Talimeren, did you try to attach a debugger to the NetWeaver yet? Can you see the values changing in the debugger or maybe the method isn't called at all? Are you using org.omg.CORBA.StringHolder?

Best regards,

Daniel

Former Member
0 Kudos

Hi Daniel Beitler thanks for your reply.

I am not sure whether you understood the problem clearly or not.

Anyway about the debugger in the netweaver, I haven't tried attaching any.

Actually I know that my function is executing properly.

I have tried out with the following example :

String mySecondFunction(StringHolder outReturn)
{
String result="Before Assignment";
outReturn.value="updated value";
result=Result+"After Assignment";
return result;
}

And I am getting the returned value of result as Before Assignment After Assignment

but my outReturn is not updated at all.

I am using javax.xml.rpc.holders.StringHolder

thanks,

talimeren

Former Member
0 Kudos

Any help??

thanks

Tali