Home arrow Java arrow 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: 5 stars5 stars5 stars5 stars5 stars / 5
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

More about methods in Java using NetBeans IDE
(Page 1 of 5 )


A downloadable file for this article is available here.

This series introduces you to learning object oriented programming in Java visually using NetBeans IDE.  In this article, I try to go in-depth on working with “methods” in Java.  I mainly try to cover the following concepts:

  • Methods returning no values
  • Methods accepting parameters
  • Methods accepting parameters and returning values

I already introduced NetBeans IDE in my first article “Developing Java Applications using NetBeans.”  Even though that article is fairly introductory, the next two articles concentrate on the basics of JFC. You can find the next two articles here and here.

If you are new to NetBeans IDE, I strongly suggest you go through the existing articles first, before proceeding with this one. If you are new to OOP in Java using NetBeans IDE, I suggest you go through my first article in this same series.  If you are new to developing Microsoft SQL Server-based Java applications, I request you go through another article of mine (of course, this is part of a series as well) called "Developing SQL Server based Java Applications using NetBeans IDE."

Methods returning no value: demo

In my previous article (the first in this series), I introduced OOP in Java with a simple example. Now, I shall use the same application and extend it as much as possible.

By now, I believe that you already know about “classes,” “instances/objects,” “members,” “fields/attributes,” “methods” and other buzz words. If not, go through my first article in this series. In this article, I shall go into detail about “methods” and their usage in Java. 

Now, open your previous application (or download it from my previous article) and open “MyCalc.java.” Modify your code so that it looks something like the following:

package MyPack;

/**
 *
 * @author Administrator
 */
public class MyCalc {   

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

    /** Creates a new instance of MyCalc */
    public MyCalc() {
    }   

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

}

Now, get back to the frame “test.java.” Double click on it to open and finally double click on the button to open source view. Within the source view, modify your “buttonActionPerformed” in such a way that it looks like the following:

private void btnShowActionPerformed(java.awt.event.ActionEvent
evt) {
// TODO add your handling code here:
        MyCalc obj1 = new MyCalc();
        MyCalc obj2 = new MyCalc();       

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

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

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

The next section will explain the above code.


blog comments powered by Disqus
JAVA ARTICLES

- Java Too Insecure, Says Microsoft Researcher
- Google Beats Oracle in Java Ruling
- 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 ...

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




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