Java
  Home arrow Java arrow Page 4 - More about methods in Java using NetBeans ...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
JAVA

More about methods in Java using NetBeans IDE
By: Jagadish Chaterjee
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 2
    2006-07-03

    Table of Contents:
  • More about methods in Java using NetBeans IDE
  • Methods returning no value: explanation
  • Methods with parameters: demo
  • Methods accepting parameters: explanation
  • A method that accepts parameters and returns a value

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    More about methods in Java using NetBeans IDE - Methods accepting parameters: explanation


    (Page 4 of 5 )

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

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

    There is no much of a difference in the “Fields.” The following is the new “method” I introduced:

        public void addValue(int v) {
            x += v;
            y += v;
        }

    The above method is named “addValue.” It returns a “void” type (which returns nothing). But it accepts a parameter “v.”  That means we need to pass some value (of type integer) when we call this method.

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

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

    I declared an object named “obj1.” Now, it internally contains five members (x, y, z, calcSum and addValue).  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 the following statement:

            obj1.addValue(2);

    You can observe that we are passing a value 2 while calling the method “addValue.”  This will be automatically passed to the variable “v,” and finally the same value gets added to both “x” and “y.”  After that, I am simply calling “obj1.calcSum.”  This method will indeed calculate the sum of “x” and “y” and place the result in “z” (which is also a member of the same object “obj1”).

    Similarly, I am also working with another object as follows:

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

    More Java Articles
    More By Jagadish Chaterjee


       · Hello guys. Another extension to the concept of "methods" in java. enjoy
     

    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...







    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 4 Hosted by Hostway
    Stay green...Green IT