On Wed, 15 Dec 1999, Singh, Sushil wrote:
> Hi Mann:
>
> Thanks for your reply.
>
> I am providing you the listing of a small program:
>
> ================= Start of program ======================
> import java.lang.*;
java.lang.* classes are always available, you don't need to import them.
> public class test
> {
> public static void main(String args[])
> {
> String str1 = "AAAAAA";
>
> method1(str1);
> System.out.println("After returning from str1: " + str1);
> }
>
> static void method1(String str1)
> {
> System.out.println("str1: " + str1);
> str1 = "xxxxxx";
> }
>
> } // End of test
> ===================== End of program ========================
[ ... ]
You can't change a String (or in fact any Object) this way (i.e. by
assigning another value to the parameter variable) because Java does
not do true pass by reference. Here is an example of a way you can
change an Object in a method with the way Java does parameter passing:
public class test
{
public static void main(String args[])
{
StringBuffer str1 = new StringBuffer("AAAAAA");
method1(str1);
System.out.println("After returning from str1: " + str1);
}
static void method1(StringBuffer str1)
{
System.out.println("str1: " + str1);
str1.setLength(0);
str1.append("xxxxxx");
}
} // End of test
str1 will be changed in main after the call to method1. But note that
I had to use a StringBuffer; you can't do this type of thing with a
String, because they are immutable.
Here is a way you can do what you want, although it's still a little
unclear why you have your functions organized this way, it would seem
there's a better way to organize them:
public class test
{
public static void main(String args[])
{
String str1 = "AAAAAA";
str1 = method1(str1);
System.out.println("After returning from str1: " + str1);
}
static void method1(String str1)
{
System.out.println("str1: " + str1);
return "xxxxxx";
}
} // End of test
And again, this question if really off-topic for this list (apologies
to everyone for responding to it).
Milt Epstein
Research Programmer
Software/Systems Development Group
Computing and Communications Services Office (CCSO)
University of Illinois at Urbana-Champaign (UIUC)
[EMAIL PROTECTED]
___________________________________________________________________________
To unsubscribe, send email to [EMAIL PROTECTED] and include in the body
of the message "signoff SERVLET-INTEREST".
Archives: http://archives.java.sun.com/archives/servlet-interest.html
Resources: http://java.sun.com/products/servlet/external-resources.html
LISTSERV Help: http://www.lsoft.com/manuals/user/user.html