Introducing Methods

By: aathishankaran Emailed: 1728 times Printed: 2329 times    

Latest comments
By: rohit kumar - how this program is work
By: Kirti - Hi..thx for the hadoop in
By: Spijker - I have altered the code a
By: ali mohammed - why we use the java in ne
By: ali mohammed - why we use the java in ne
By: mizhelle - when I exported the data
By: raul - no output as well, i'm ge
By: Rajesh - thanx very much...
By: Suindu De - Suppose we are executing

Introducing Methods

Classes usually consist of two things:- Instance variable and Methods. The topic of methods is a large one because java gives them so much power and flexibility. However, there are some fundamentals that you need to learn now so that you can begin to add methods to your classes.

           This is the general form of a method:

             Type name( parameter-list) {

                        //body of method

}

             Here, type specifies the type of data returned by the method. This can be any valid type, including class types that you create. If the method does not return a value, its return type must be void. The name of the method is specified by name. This can be any legal identifier other than those already used by other items within the current scope. The parameter-list is a sequence of type and identifier pairs separated by commas. Parameters are essentially variables that receive the value of the arguments passed to the method when it is called. If the method has no parameters, then the parameter list will be empty.

             Methods that have a return type other than void return a value to the calling routine using the following form of the return statement:

             return value;

             Here, value is the value returned.

 Adding a method to a class

             Although it is perfectly fine to create a class that contains only data, it rarely happens. Most of the time you will use methods to access the instance variables defined by the class. In fact, methods define the interface to most classes. This allows the class implementor to hide the specific layout of internal data structures behind cleaner method abstractions. In addition to defining methods that provide access to data, you can also define methods that are used internally by the class itself.

             Let’s begin by adding a method to the test class. It may have occurred to you while looking at the preceding programs that the computation of a summation was something that was best handled by the test class rather than the testdemo class. After all, since the volume of a box is dependent upon the values, it makes sense to have the test class compute it. To do this, you must add a method to test class, as shown here:

 class Test {

      double item1;

     double item2;

     double item3;

     void value() {

          System.out.println(“value is”);

          System.out.println( item1 + item2 + item3);

     }

}

class Testdemo() {

     public static void main (String args[]) {

          Test test1 = new test(); 

          Test test2 = new test();       

 

          test1.item1 = 10;

          test1.item2 = 20;

          test1.item3 = 15;

 

          test2.item1 = 3;

          test2.item2 = 6;

          test2.item3 = 9;

         

          test1.value();

 

          test2.value();

     }

}

 

This program generates the following output,

            value is 45.0

     value is 18.0

Look closely at the following two lines of code:

     test1.value();

     test2.value();

     The first line here invokes the value () method on test1. That is, it calls value () relative to the test1 object, using the object’s name followed by the dot operator. Thus, the call to test1.value() displays the value of the members defined by test1 and the call to test2.value() displays the value of the members defined by test2. Each time value () is invoked, it displays the volume for the specified data member.


Java Home | All Java Tutorials | Latest Java Tutorials

Sponsored Links

If this tutorial doesn't answer your question, or you have a specific question, just ask an expert here. Post your question to get a direct answer.



Bookmark and Share

Comments(0)


Be the first one to add a comment

Your name (required):


Your email(required, will not be shown to the public):


Your sites URL (optional):


Your comments:



More Tutorials by aathishankaran
Web Security Issues
The Web User's Perspective
Server-side plug-Ins
The best way to avoid security vulnerabilities with new server
JavaScript Security
Window Object
Working with Status Bar Messages
Retrieving a Portion of a String
Referencing Windows
Math Object
Frame Object
Document Object
Closing Windows
Built-in Object in Javascript
Textarea Object

More Tutorials in Java
Update contents of a file within a jar file
Tomcat and httpd configured in port 8080 and 80
Java File
Java String
Count number of vowels, consonants and digits in a String in Java
Reverse a number in Java
Student marks calculation program in Java
Handling Fractions in Java
Calculate gross salary in Java
Calculate average sale of the week in Java
Vector in Java - Sample Program
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
Java program to check if user input is an even number

More Latest News
Most Viewed Articles (in Java )
How to Send SMS using Java Program (full code sample included)
The Basic Structure of a Simple Java program
XML and Java - Parsing XML using Java Tutorial
What is Java?
Method Overloading (function overloading) in Java
Method Overriding in Java
How to use ArrayList in Java
FileReader and FileWriter example program in Java
Vector example in Java
Simple Port Scanner application using Java
append() in Java
Read from a COM port using Java program
Calendar - sample program in Java
Count number of vowels, consonants and digits in a String in Java
indexOf( ) and lastIndexOf( ) in Java
Most Emailed Articles (in Java)
Vector in Java - Sample Program
Configuring a DataSource in Tomcat
Applet Notinited : Applet xxxxxxxxxxxx notinited
Use of 'finally' in Java
Basics of Exception Handling in Java
Tomcat and httpd configured in port 8080 and 80
Calculate average sale of the week in Java
MultiLevel Inheritance sample in Java
Multiple Inheritance sample in Java
Java program using Method Overriding
LinkedList in Java
Taking the size of an Array at runtime & generate random numbers to populate the Array
Getting Started with Java
Creating Users and Passwords with Tomcat using tomcat-users.xml
What is JasperReports?