my dog learned polymorphism*
The moose likes Beginning Java and the fly likes quick question about static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "quick question about static" Watch "quick question about static" New topic
Author

quick question about static

Jack Adams
Greenhorn

Joined: Jun 08, 2004
Posts: 25
private void TestStatic (String[] myString)
{
}

call
String dontChange[] = new String[10];
TestStatic (dontChange);

In the above example TestStatic CAN alter contents of original array.
So how would I make TestStatic non-destructive, where it is not possible to change dontChange array?
Mukesh Mittal
Ranch Hand

Joined: Jan 13, 2006
Posts: 31
Try to use cloning. If you are calling this function from outside, please change it to public.


example"
int[] primes = {1,2,3}
backup = (int[]) primes.clone();
Jack Adams
Greenhorn

Joined: Jun 08, 2004
Posts: 25
I was thinking more in line with C++. Where I can make what is being pointed to static. or immutable?
karthikeyan Chockalingam
Ranch Hand

Joined: Sep 06, 2003
Posts: 259
When we pass the values as Array, we cannot prevent the modification.(This is the reason when changes in values to primitives in the called method should reflect in the caller method, the primitive values are wrapped in an Array and passed).


http://www.skillassert.com


Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Also note, Jack, that although the word "static" means several different things in Java and C++, "constant" isn't really one of them. "const" is the term in C++, and "final" is the closest equivalent in Java. "static" means something else altogether.

Now, as to your question: In C++, if you declare a const pointer:

int* const p;

then you can't modify the pointer p, although you can modify what p points to. Java's "final" has the same effect on an array declaration. You can modify the array contents, but not the variable that points to the array.

If, on the other hand, in C++ you declare a pointer-to-const:

const int * p;

then you can change p, but not what p points to. Java has no equivalent to this: the contents of a Java array are never const.


[Jess in Action][AskingGoodQuestions]
Jack Adams
Greenhorn

Joined: Jun 08, 2004
Posts: 25
not what it points to!?
so one can never be sure after passing array (or even any object by reference) in java that your original data will not changed. ie no contract using compiler?
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

A client can't be absolutely sure in C++, either, can it? -- because a method can always "cast away const."

But in any case, you've already gotten the standard advice, which is that you have to copy an array and pass the copy if you need to be sure a method won't change it. As far as other objects, you can play games with interfaces: a method might accept an argument of type ConstFoo (an imaginary interface with only "const methods.") There might be another interface Foo which extends ConstFoo and adds mutator methods. You could then pass a Foo to that method, knowing that the method would see it as a ConstFoo, and not want to call any of the mutating methods of a full Foo. But of course, just as in C++, the method could cheat and cast the ConstFoo to a Foo.
Jack Adams
Greenhorn

Joined: Jun 08, 2004
Posts: 25
Just chkd out some websites. So this is the Java way (from 1st example)
private void TestStatic (String[] myString)
{
String[] localString = (String[]) (myString.clone ());

}

call
String dontChange[] = new String[10];
TestStatic (dontChange);

wow, this is different...
Jeff Albertson
Ranch Hand

Joined: Sep 16, 2005
Posts: 1780
Originally posted by Ernest Friedman-Hill:
As far as other objects, you can play games with interfaces: a method might accept an argument of type ConstFoo (an imaginary interface with only "const methods.") There might be another interface Foo which extends ConstFoo and adds mutator methods. You could then pass a Foo to that method, knowing that the method would see it as a ConstFoo, and not want to call any of the mutating methods of a full Foo. But of course, just as in C++, the method could cheat and cast the ConstFoo to a Foo.


It should be noted, that in Java and C++, passing a proxy is a good way to avoid this kind of cheating:


There is no emoticon for what I am feeling!
 
 
subject: quick question about static
 
Similar Threads
compile error: non-static reference (simple question)
doubt pls!!
There are some things that only the Creator(s) can explain !
Static Variable Code question..
static variables are not accesed by class instances????