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.
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();