Home arrow Java arrow Page 2 - More about methods in Java using NetBeans IDE
JAVA

More about methods in Java using NetBeans IDE


If you want to go past the basics of working with methods in Java, this article is for you. You will learn about methods returning no values, methods accepting parameters, and more.

Author Info:
By: Jagadish Chaterjee
Rating: 4 stars4 stars4 stars4 stars4 stars / 3
July 03, 2006
TABLE OF CONTENTS:
  1. · More about methods in Java using NetBeans IDE
  2. · Methods returning no value: explanation
  3. · Methods with parameters: demo
  4. · Methods accepting parameters: explanation
  5. · A method that accepts parameters and returns a value

print this article
SEARCH DEVARTICLES

TOOLS YOU CAN USE

advertisement
More about methods in Java using NetBeans IDE - Methods returning no value: explanation
(Page 2 of 5 )

This section explains the code listed in the previous section. Let us start with the following:

    int x=0;
    int y=0;
    int z=0;

Now, you can observe that I declared three fields/attributes for the class “MyCalc.” What follows is the new “method” I introduced:

    public void calcSum() {
        z = x + y;
    }

The above method is named “calcSum” and it returns a “void” type. The “void” indeed is simply “nothing.”  In other words, the method “calcSum” (unlike our previous “getSum” method) doesn’t return any value after its calculation.

Now, if we proceed to our “test” frame, we have the following initially:

        MyCalc obj1 = new MyCalc();
        obj1.x = 10;
        obj1.y = 20;
        obj1.calcSum();

I declared an object named “obj1.”  Now, it internally contains four members (x, y, z, calcSum). You can also observe that I am simply providing the values for “x” and “y” (but not “z”). After assigning the values to “x” and “y,” I am simply calling “calcSum.”  This method will indeed calculate the sum of “x” and “y” and place the result in “z” (which is also a member to the same object “obj1”).

You must also observe that I am not taking (or assigning) any value from “calcSum,” as it doesn’t return anything at this moment. Similarly, I am also working with another object as following:

        MyCalc obj2 = new MyCalc();
        obj2.x = 100;
        obj2.y = 200;
        obj2.calcSum();

Once the calculations are over, I display the values of “z” in each of those objects with the following two statements:

        this.lblMsg.setText("Sum = " + String.valueOf(obj1.z));
        this.lblMsg2.setText("Sum = " + String.valueOf(obj2.z));


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 6 - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials