Java Instance Methods are functions that can be called on an object without needing to instantiate a new object. They’re useful when we need to perform the same operation across multiple objects in our application and don’t want or need them linked together through references (which is how Java’s OOP works).
Instance methods are a type of method in Java that can be called on an object. The instance method is defined within the class, and not by the object itself. The following example will show how to use an instance method from a class.
This article will teach you how to:
- Explain the purpose of instance methods and the rules for naming them.
- Explain how to use variable-argument methods.
Instance Methods Concept
The actual implementation of an operation on an object is specified as an instance method. It describes the stages or method in which the desired action should be performed.
A method that is called by an instance and has access to instance variables is known as an instance method. When naming a method, the following standards must be followed:
- This isn’t a Java keyword.
- Spaces are not allowed.
- You can’t start with a digit.
- Can start with a letter, underscore, or the dollar sign ($).
- It should be a lowercase verb.
- It should be both detailed and informative.
- It should be a multi-word name that starts with a lowercase verb and then moves on to adjectives, nouns, and so on.
The instance methods defined in the Student class are called by all instances of the class, as shown in the diagram below.
The syntax for defining an instance method in a class is as follows.
([parameters list]) / This is the method’s body.
where, returntype: The data type of the value returned by the method is specified here. list of parameters: are the values given to the method method name: is the method name list of parameters: is the method name list of parameters: is the method name list of parameters: is the method name list of parameters:
The following code shows how to declare instance methods inside the Student class.
Snippet of code:
/ Student is a class declaration / Instance variable int age; / Instance methods int getAge() / Getting the age of an instance variable; age = I void setAge(int I main public static void (String args) Student stud = new Student(); stud.age = 24; / Using the dot operator to invoke the instance function via an object int resultage = stud.getAge(); System.out.println(“The student’s age is:” + resultage);
The Student class has an instance variable, age, and two instance methods, getAge() and setAge(), as seen in the code (). For the class,Student, the main() function generates an instance called stud. The stud instance accesses the age variable and sets it to 24. Because instance methods have access to variables defined inside the class, a call to the getAge() function returns the value of variable age, which is then saved in a local variable called resultage.
The student is 24 years old.
Method Comments in Javadoc
Note: The following are the Javadoc comments for methods in Java:
/** * Method description: Javadoc comments on the method’s description. The @param tag is followed by the parameter’s name (not data type) and description. The @return tag is used to define the method’s return type. It’s not applicable to methods with a void return type.
After the return statement, statements in the current method are skipped, and control returns to the statement that called the method. Use the form of return statement that does not return any value with methods that are declared void:
A void method may be required to return explicitly if a condition is satisfied. This kind of return statement may be utilized in this scenario. Consider the following scenario:
/ Assumes age is an instance variable in an already created void class if (age10) returns, setAge(int age); this.age=age;
Arguments are passed on the basis of their value.
The ability to pass parameters by value is available in Java. This implies that if the calling method’s value is given as an argument to the called method, any changes made to the passed value in the called method will not affect the calling method’s value. Value is provided to variables of basic data types like int and float. This is a secure method of passing a parameter.
The following code shows how to use the pass by value method to send parameters to the called method, checkVal().
Snippet of code:
public static void main public class PrimitiveDatatypeClass (String args) int I = 3; /invoke checkVal() with I as an argument checkVal(i); / print I to see what it’s worth / parameterized method / change parameter value in checkVal() public static void checkVal(int j) j=j+5; System.out.println(“After calling checkVal, I = ” + I / parameterized method / change parameter value in checkVal() public static void checkVal(int j) j=j+5;
I = 3 after calling checkVal
Using References to Pass Arguments
The called method may alter the value of the arguments supplied to it from the calling method using call by reference. Parameters of the reference data type are provided to the methods as a value rather than a reference. This implies that the passed-in reference still points to the same object when the method returns. The values of the instance variables, on the other hand, may be modified inside the method.
The caller may modify the values saved but not the reference variables when references are provided as arguments. The following code shows how to use the pass by reference method to provide parameters to the called function, drawLine().
Snippet of code:
line.setX(line.getX + pointX); line.setY(line.getY + pointY); /code to assign a new reference to line line = new Line(0, 0); public void drawLine(Line line, int pointX, int pointY) line.setX(line.getX + pointX); line.setY(line.getY + pointY);
The following are the parameters that must be sent to the method:
drawLine(myLine, 3, 6); obj.drawLine(myLine, 3, 6);
Argument Methods with Variables
Calling a method with a variable number of parameters is possible using variable arguments. When the amount of arguments to be given to a method is unknown when the method is written, this is utilized.
The method declaration with variable number of parameters is shown in the diagram below.
The syntax for defining a method with a variable number of parameters is as follows.
(type… variableName) / body of the function
The ellipsis (…) is used to indicate that the number of parameters is changeable. The code below shows how to declare a method with a variable number of parameters inside a class.
Snippet of code:
public static void dispNumber(int…num) for (int I num) System.out.println(“Model Number is ” + I + “. “); public static void main(String args) dispNumber(201, 301); public static void main(String args) dispNumber(201, 301);
201 is the model number. 301 is the model number.
The for (int i:num) statement was added in J2SE 5.0 as an improved version of the for loop. Variable parameters benefit greatly from this kind of for loop expression. The statement enables me to iterate through a list of variable parameters, namely, num.
An instance method is a special type of method defined within an object. Instance methods are invoked on the object itself, and not on any other objects in the program. Reference: instance method example.
Frequently Asked Questions
What are instance methods Java?
What is meant by instance methods?
A: Instance methods are methods that exist for each particular instance of an object. These instance methods can be passed around like any other function, and they will behave differently depending on the context in which theyre called.
What are the types of instance method?
- static method in java with example
- java static method
- how to call an instance method in java
- instance method vs class method java
- static method vs instance method