• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Peter Rooke
  • Himai Minh
Bartenders:
  • Piet Souris
  • Mikalai Zaikin

quick question about static

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 31
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Ranch Hand
Posts: 259
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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).
 
author and iconoclast
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24204
44
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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...
 
Ranch Hand
Posts: 1780
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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:
 
Consider Paul's rocket mass heater.
reply
    Bookmark Topic Watch Topic
  • New Topic