Methods in Java
By: aathishankaran in Java Tutorials on 2007-03-06
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 us 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.
Add Comment
This policy contains information about your privacy. By posting, you are declaring that you understand this policy:
- Your name, rating, website address, town, country, state and comment will be publicly displayed if entered.
- Aside from the data entered into these form fields, other stored data about your comment will include:
- Your IP address (not displayed)
- The time/date of your submission (displayed)
- Your email address will not be shared. It is collected for only two reasons:
- Administrative purposes, should a need to contact you arise.
- To inform you of new comments, should you subscribe to receive notifications.
- A cookie may be set on your computer. This is used to remember your inputs. It will expire by itself.
This policy is subject to change at any time and without notice.
These terms and conditions contain rules about posting comments. By submitting a comment, you are declaring that you agree with these rules:
- Although the administrator will attempt to moderate comments, it is impossible for every comment to have been moderated at any given time.
- You acknowledge that all comments express the views and opinions of the original author and not those of the administrator.
- You agree not to post any material which is knowingly false, obscene, hateful, threatening, harassing or invasive of a person's privacy.
- The administrator has the right to edit, move or remove any comment for any reason and without notice.
Failure to comply with these rules may result in being banned from submitting further comments.
These terms and conditions are subject to change at any time and without notice.
- Data Science
- Android
- React Native
- AJAX
- ASP.net
- C
- C++
- C#
- Cocoa
- Cloud Computing
- HTML5
- Java
- Javascript
- JSF
- JSP
- J2ME
- Java Beans
- EJB
- JDBC
- Linux
- Mac OS X
- iPhone
- MySQL
- Office 365
- Perl
- PHP
- Python
- Ruby
- VB.net
- Hibernate
- Struts
- SAP
- Trends
- Tech Reviews
- WebServices
- XML
- Certification
- Interview
categories
Related Tutorials
Read a file having a list of telnet commands and execute them one by one using Java
Open a .docx file and show content in a TextArea using Java
Step by Step guide to setup freetts for Java
Of Object, equals (), == and hashCode ()
Using the AWS SDK for Java in Eclipse
DateFormat sample program in Java
concurrent.Flow instead of Observable class in Java
Calculator application in Java
Sending Email from Java application (using gmail)
Comments