Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Beginning Java and the fly likes create instance variable Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "create instance variable" Watch "create instance variable" New topic
Author

create instance variable

sathi jogi
Greenhorn

Joined: Aug 31, 2007
Posts: 14
How to create an array instance variable, so that it will be allowed to modify only with in that instance object and visible to other objects but it should not allow to modify that array object values by other instance objects?
Peter Chase
Ranch Hand

Joined: Oct 30, 2001
Posts: 1970
I am struggling to understand the question you have posed, but I think you are asking how one can give access to an array, but not allow the contents of the array to be changed.

If that is the question, the answer is you can't.

If you have a reference to a Java array, you can always modify the contents of the array. Java arrays are low-level things, more suited to private implementation details of a class than for passing around between classes. You should use classes, including Collections(*), in preference to arrays, most of the time.

If you want to provide the sort of facilities you're talking about, you should wrap the array up in a class. The array would be a private instance variable of the class. The class would then provide methods to allow appropriate access to the data in the array (e.g. private setters, public getters).

(*) Of course, Collections are mostly implemented using arrays internally. But that's the sort of private implementation detail for which arrays are well-suited.
[ September 05, 2007: Message edited by: Peter Chase ]

Betty Rubble? Well, I would go with Betty... but I'd be thinking of Wilma.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: create instance variable