In object oriented programming, you consider the object which you are programming about, for example, a car. You then consider the attributes about the object, for example, the make, model, color, year built, etc.
You also have consider the operations of the object. i.e. What can it do? A car for example could turn on cruise control, start/accelerate, stop/break, turn, etc. Each of these will be one or more methods which can be called to control, simulate, etc that object.
Additionally, you will have methods to allow you to access the private attributes, usually through getter and setter methods.
Each of these methods will basically be created in the same manner, so let’s start to look at some examples, by first looking at a general form.
<accessor> <static - optional> <return type> <method name>(<parameter list - optional>) {
<method body>
}
Accessor
This is the privacy accessor, as in who can access this method. There are four levels of accessor security, and they are the same for methods as they are for instance variables.
- private – only this class can access it
- protected – this class, and children of this class can access it
- package – default – any class/object within the same package can access it
- public – most common for methods, anyone can access it.
static
Static is an optional keyword. If the method is static, then someone can call the method without having an object defined. This is how the main method works.
return type
What will the method return. It may return nothing, in which case the void keyword is used. However, it can return any primitive, or non-primitive datatype including user created classes.
Method Name
This identifier is uses to name the method so you can call it later on.
The method name cannot be a reserved word, and must follow the other rules for naming, just like when you name a variable. That includes:
- must start with a letter,
- can contain only letters, numbers, and underscores, and
- is case sensitive.
Naming convention encourages, but doesn’t require, camel case style notation.
Typically you should focus on making your method name explain what it is your method is doing.
Method Body
Within the body, you can have any legal Java code, including declaring local variables, conditions, loops, and calling other functions.
Method Examples
public void setAnswer(int newAnswer) {
this.answer = newAnswer;
}
public static int getCount() {
return count;
}
protected Object chooseRandomListElement(int seedNumber ) {
// perform action to get a random list element
}
Overloading a Method
You can have overloaded methods, just like you can overload functions in C++. We’ll look at this in more detail next.
What’s Missing
If you are familiar with C/C++, you might feel like something is missing. And you are correct, there are no header files and no function prototypes in Java. Since Java came out after C++, when computers were becoming more powerful, the Java Compiler is smart enough to figure out what a method looks like, which method is being called, etc, without any type of prototypes.
Many Java developers like this as it simplifies the code writing process for them.
Calling a Method
Calling a method, is as simple as calling the object from which is was created, then putting a dot (.
), and finally passing the required parameters.
Sample1 s1 = new Sample1();
s1.setValue(5); // call an instance method of s1, and pass it the value 5
You can call other methods within your method of course, using the same technique. This also includes recursive calls, where your method calls itself.
public static void recursiveExample(int counter) {
if (counter > this.maxRecursionCalls) {
return;
}
// do other things...
this.recursiveExample(counter + 1);
}
Class Methods in Java was originally found on Access 2 Learn
2 Comments
Comments are closed.