File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Beginning Java and the fly likes using set and get methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Murach's Java Servlets and JSP this week in the Servlets forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "using set and get methods" Watch "using set and get methods" New topic
Author

using set and get methods

basia fish
Greenhorn

Joined: Aug 04, 2003
Posts: 17
hi

hor do i use accessor and mutator methods with an array
does this looks ok

private int arr[];

public void setArr()
{
arr = new int[1];
}

public int[] getArr()
{
return arr;
}

thanks for any help
basi
Chengwei Lee
Ranch Hand

Joined: Apr 02, 2004
Posts: 884
Not quite right. It really depends on what you wanna do with the array. Do you wanna return an element value from the specific index? Or a fixed location everytime?

When you uses a setter method, where do you wanna set the new data into? A fixed position or an arbitrary position?

Usually, not always, setter methods have to take in a parameter, usually the data you wishes to assign to a variable. And a getter method has no arguments while returning the value that you're interested in obtaining.

Hope this helps.


SCJP 1.4 * SCWCD 1.4 * SCBCD 1.3 * SCJA 1.0 * TOGAF 8
basia fish
Greenhorn

Joined: Aug 04, 2003
Posts: 17
would that be more appropriate

private int arr[];

public void setArr(int bArr[])
{
for (int i=0; i<aArr.length-1; i++)
arr[i] = aArr[i];
}

public int[] getArr()
{
if (arr==null)
arr=new int[10];
return arr;
}


thanks for help
Gabriel White
Ranch Hand

Joined: Mar 02, 2003
Posts: 233
Like Cheng stated, it depends on your application. Whether you are sorting or adding or indexing...etc the array then you would have different accessor and mutator methods. It looks like the methods that you have created are just putting numbers in the index and then you are trying to return the numbers. Remember that int is a primitive type and when you try to print out this list of numbers that you are adding in you will just get a memory location for the entire array. If you want to see the numbers then just change the get method to SOP the arr[i]. I would suggest something like this:



HTH

Gabe
[ June 16, 2004: Message edited by: Gabriel White ]
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: using set and get methods
 
Similar Threads
Problem in Arrays passing.
doubt in inheritance...
for loop and arrays
Can you please explain how output is 0 0 3 0
Arrays