accessing class fields


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 6 of 6

Thread: accessing class fields

  1. #1
    Join Date
    Mar 2005
    Posts
    4

    Smile accessing class fields

    say i have

    public class test
    {
    public int a;
    }

    public class changeValue
    {
    public changeValue()
    {
    test[] b = new test[5];
    for (int i=0;i<5;i++)
    // I want to edit the value of 'a', for each instance of 'b'
    }
    }

    how can this be done?

  2. #2
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    b[i].a = whatever should do it

    i.e.

    Code:
    public class changeValue
    {
        public changeValue()
        {
            test[] b = new test[5];
            for (int i=0;i<5;i++)
            {
                // I want to edit the value of 'a', for each instance of 'b'
                b[i].a = 1;
            }
        }
    }
    It is bad practice however to access "a" directly, should use a setter which could
    then be validated providing an interface to the class.
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

  3. #3
    Join Date
    Mar 2005
    Posts
    4
    I know that it is bad practice, but it's something i have to do. i assumed that b[i].a=what ever; would work, but for some reason i get a null error. if if i put a method inside the class with a, to change a, i get an error. Both of these work correctly if i dont have b as an array. why is this? a is not static, so each instance of b should have it's own value for a.

  4. #4
    Join Date
    May 2004
    Location
    Durham, UK
    Posts
    174
    Hi,

    I didn't mean to cause any upset, but as you know doubt know, sometimes you need to state the obvious - from your reply however this wasn't one of those times. Please accept my apologies.

    Back to the problem in hand.

    I think that the line test []b = new test[5] just allocates the array and
    not the elements pointed to by the array.

    This code looks to work ok

    Code:
    public class changeValue
    {
        public static void main(String[] args)
        {
            changeValue cv = new changeValue();
            
        }
        
           
        public changeValue()
        {
            test []b = new test[5];
            for (int i=0;i<5;i++)
            {
                // I want to edit the value of 'a', for each instance of 'b'
                b[i] = new test();
                b[i].a = i;
            }
            
            for (int i=0;i<5;i++)
            {
                // I want to edit the value of 'a', for each instance of 'b'
                System.out.println(b[i].a);
            }
            
        }
    }
    Please come back if you have any further difficulties.
    Hope this helps
    Graham

    Before you criticize someone, you should walk a mile in their shoes. That way, when you criticize them, and if they get mad, you are a mile away and you have their shoes ;-)

    http://www.grahamrobinsonsoftware.com

  5. #5
    Join Date
    Mar 2005
    Posts
    4
    Thx, that helped.

  6. #6
    Join Date
    Mar 2005
    Location
    Sendling, MUC, .de
    Posts
    100
    Those arrays seem to pose problems - maybe you want to also see here.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center