Win a copy of Mesos in Action this week in the Cloud/Virtualizaton forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

quick question about static

 
Jack Adams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 31
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was thinking more in line with C++. Where I can make what is being pointed to static. or immutable?
 
karthikeyan Chockalingam
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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).
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jack Adams
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24211
35
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1780
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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:
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic