Home arrow Java arrow Page 5 - Working with Arrays of Objects with Java and NetBeans IDE
JAVA

Working with Arrays of Objects with Java and NetBeans IDE


In this article, I shall discuss the following topics on programming with “methods” in Java using NetBeans IDE: overloading methods (method overloading), and working with arrays of objects (object arrays).

Author Info:
By: Jagadish Chaterjee
Rating: 3 stars3 stars3 stars3 stars3 stars / 6
July 10, 2006
TABLE OF CONTENTS:
  1. · Working with Arrays of Objects with Java and NetBeans IDE
  2. · Overloading Methods in Java: Demo
  3. · Overloading Methods in Java: Explanation
  4. · How to Create Arrays of Objects in Java: Demo
  5. · How to Create Arrays of Objects in Java: Explanation
  6. · Passing Arrays of Objects to Methods in Java: Demo

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
Working with Arrays of Objects with Java and NetBeans IDE - How to Create Arrays of Objects in Java: Explanation
(Page 5 of 6 )

There is nothing special in the class “MyCalc.” We need to focus only on the testing code. Let us go through the code step by step. 

        MyCalc[] ar = new MyCalc[10];

The above statement will simply create ten locations to store objects of type “MyCalc.” The important issue is that it creates only the location, but not the objects. Each of those locations can hold an object of type “MyCalc.” Proceeding further, we have the following:

        for(int i=0;i<10;i++) {

The above loop simply iterates from zero through nine.  Next, we have the following statement:

            ar[i] = new MyCalc();

The above statement creates a new instance/object of the class “MyCalc” and stores that instance at the ‘i’th location in array “ar.” Proceeding further, we have the following:

            ar[i].setValues(i*10,i*10+1);

Once the object is successfully created (at some location in the array), we need to set some values to its members. The same is achieved through the above statement. 

Now it is time to display the sum of all values (in all objects) available in the array. Again, we need to iterate through all the objects in the array and sum up those values. The same is achieved through the following:

        int s=0;
        for(int i=0;i<10;i++) {
            s += ar[i].getSum();
        }

We finally display the sum using the following:

        this.lblMsg.setText("Sum = " + String.valueOf(s));


blog comments powered by Disqus
JAVA ARTICLES

- Deploying Multiple Java Applets as One
- Deploying Java Applets
- Understanding Deployment Frameworks
- Database Programming in Java Using JDBC
- Extension Interfaces and SAX
- Entities, Handlers and SAX
- Advanced SAX
- Conversions and Java Print Streams
- Formatters and Java Print Streams
- Java Print Streams
- Wildcards, Arrays, and Generics in Java
- Wildcards and Generic Methods in Java
- Finishing the Project: Java Web Development ...
- Generics and Limitations in Java
- Getting Started with Java Web Development in...

Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 



© 2003-2012 by Developer Shed. All rights reserved. DS Cluster 10 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials