Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Question about arrays.

    Lets say I create an array of objects.

    Code:
    ObjType[] objArray = new objType[10];
    for (int i = 0; i < 10; i++){
    			objArray[i] = new objType(a,b);
    		}
    Where a and b are integers, and objType has a constructor which adds up a and b and stores the total in an int value called total. How can I, if at all, get the value of total for individual array objects? I tried: objArray[i].total and a number of other things, but none worked. I tried to find something on this on google but I don't really know what it is exactly that I'm searching for. Is there some specific thing I need to do or have I completely misunderstood the way arrays work?

  • #2
    Senior Coder shyam's Avatar
    Join Date
    Jul 2005
    Posts
    1,563
    Thanks
    2
    Thanked 163 Times in 160 Posts
    Quote Originally Posted by Dustfinger View Post
    Where a and b are integers, and objType has a constructor which adds up a and b and stores the total in an int value called total. How can I, if at all, get the value of total for individual array objects? I tried: objArray[i].total and a number of other things, but none worked.
    accessing the value of a member variable depneds on the access specifier used like private, public, protected or none (package).
    You never have to change anything you got up in the middle of the night to write. -- Saul Bellow

  • #3
    Regular Coder brad211987's Avatar
    Join Date
    Sep 2005
    Location
    Ohio
    Posts
    631
    Thanks
    10
    Thanked 50 Times in 50 Posts
    The typical way to do this is to provide accessor methods in your objects class. Here is an example object class as you described with accessor methods:

    PHP Code:
    public class MyObject
    {
        private 
    int total;

        public 
    MyObject (int xint y)
        {
            
    total y;
        }

        public 
    int getTotal()
        {
            return 
    total;
        }

    Now in your code where you create and call the object:

    PHP Code:
    MyObject obj = new MyObject(56);
    int total obj.getTotal();  //total is assigned the value of 11(5 + 6) 
    You can use the same method with arrays by using a call such as obj[i].getTotal();

    What happens here, is in your objects class, the variable total is a private instance variable, meaning that it can only be directly accessed by methods of that same class. The accessor method getTotal() accesses that variable, and returns its value to the caller of the method. Note that the getTotal() method has a public type, so that it can be called by any other class that creates a reference to the MyObject class.

  • Users who have thanked brad211987 for this post:

    Dustfinger (02-23-2008)

  • #4
    New Coder
    Join Date
    Jun 2007
    Posts
    19
    Thanks
    2
    Thanked 0 Times in 0 Posts
    Alright, I got it working now. I hadn't thought about whether or not total was even able to be accessed publicly, I just thought I had the wrong syntax or something like that. Thank you brad for the explanation.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •