Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 3

What is the role of Java methods and properties?

What do they describe and how can they be


used? Please give examples.

Java method is a set of instructions that preforms an individual action. It is also a collection of
statements that are grouped together to perform an operation. A method must be declared within a
class. It is defined with the name of the method, followed by parentheses. Java methods usually look
like:

public static int methodName(int a, int b) {


// body
}

The analysis of the above relationship can be found below:

- public static is a modifier


- int is a return type
- methodName is the name of each method
- a, b are formal parameters
- int a & int b are list of parameters

In order to use a method, we need to call it. There are two ways in which a method is called:

- When a method returns a value


- When a method returns nothing

The general format of a Java method call is: methodName(argumentList)where the argumentList
is a comma-separated list of arguments (data being sent into the method)..

The calling process of a method is simple. When a program invokes a method, the program control
gets transferred to the called method. This called method then returns control to the caller in two
conditions:

- When the return statement is executed


- When the end of the process is reaching

The methods returning void is considered as call to a statement.

PROPERTIES

Properties is a subclass. It is used to maintain lists of values in which the key is a String and the
value is also a String. The Properties class is used by many other Java classes. For example, it is
the type of object returned by System.getProperties( ) when obtaining environmental values.

Properties define the following instance variable. This variable holds a default property list
associated with a Properties object. The properties object contains key and value pair both as a
string. It can be used to get property value based on the property key. The Properties class provides
methods to get data from properties file and store data into properties file. Moreover, it can be used
to get properties of system.

A good advantage of properties is that in case some information change from the properties file,
we don't need to recompile the java class. It is used to store information which is to be changed
frequently.

An example of properties is the below(tutorialspoint, na):


import java.util.*;
public class PropDemo {

public static void main(String args[]) {


Properties capitals = new Properties();
Set states;
String str;

capitals.put("Illinois", "Springfield");
capitals.put("Missouri", "Jefferson City");
capitals.put("Washington", "Olympia");
capitals.put("California", "Sacramento");
capitals.put("Indiana", "Indianapolis");

// Show all states and capitals in hashtable.


states = capitals.keySet(); // get set-view of keys
Iterator itr = states.iterator();

while(itr.hasNext()) {
str = (String) itr.next();
System.out.println("The capital of " + str + " is " +
capitals.getProperty(str) + ".");
}
System.out.println();

// look for state not in list -- specify default


str = capitals.getProperty("Florida", "Not Found");
System.out.println("The capital of Florida is " + str + ".");

The above code will give us the below results:

The capital of Missouri is Jefferson City.


The capital of Illinois is Springfield.
The capital of Indiana is Indianapolis.
The capital of California is Sacramento.
The capital of Washington is Olympia.

The capital of Florida is Not Found.

REFERENCES

- Functions. Retrieved 11 December 2019, from


http://www.cs.fsu.edu/~myers/cgs3416/notes/methods.html
- Java - The Properties Class - Tutorialspoint. Retrieved 11 December 2019, from
https://www.tutorialspoint.com/java/java_properties_class.htm
- Java Methods. Retrieved 11 December 2019, from
https://www.w3schools.com/java/java_methods.asp

You might also like