0% found this document useful (0 votes)
32 views4 pages

Lesson 23

Instance methods require an object of their class to be created before being called. They exist as multiple copies depending on the number of instances. Instance methods can access instance variables and methods directly. Static methods exist as a single copy for a class and can be accessed without creating an object. They cannot access instance variables or methods directly but must use a reference to an object. The 'this' keyword refers to the current object and is used to avoid naming conflicts in methods.

Uploaded by

Debadrito Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views4 pages

Lesson 23

Instance methods require an object of their class to be created before being called. They exist as multiple copies depending on the number of instances. Instance methods can access instance variables and methods directly. Static methods exist as a single copy for a class and can be accessed without creating an object. They cannot access instance variables or methods directly but must use a reference to an object. The 'this' keyword refers to the current object and is used to avoid naming conflicts in methods.

Uploaded by

Debadrito Ray
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

COMPUTER APPLICATIONS Class X

TOPIC :- Methods( Lesson 23)

COMPUTER APPLICATIONS
CLASS – X

TOPIC :-Methods (Lesson 23)

Instance Method

Instance method are methods which require an object of its class to be created
before it can be called. To invoke an instance method, we have to create an
Object of the class in within which it defined.

public class Nonstatic {


public int show(int x) { // with arguments
x+=x++*5;
return x;
}
public static void main(String args[]) {
Nonstatic test = new Nonstatic();
int a=10;
int y = test.show(a);
System.out.println("Value before performing task” +a);
System.out.println("Value after performing task” +y);
}
}

Instance method vs Static method

In Java as we know that the behaviour of any variable/method is defined by the


keyword that is used in front of its declaration name. So one of the modifiers is
Static which can be used along with method as well as with variable.
Static methods as name states defined at the class level and could be accessed
on the class name i.e. no need of class object creation in order to access/call the
static methods.
While on another hand if we do not uses the static keyword with variable/method
than it belongs or categorized as instance method which is defined at instance
level and need class object for their accessibility.

Page 1
COMPUTER APPLICATIONS Class X
TOPIC :- Methods( Lesson 23)

Also static methods exist as a single copy for a class while instance methods
exist as multiple copies depending on the number of instances created for that
particular class.
Instance method can access the instance methods and instance variables
directly. Instance method can access static variables and static methods directly.
Static methods can access the static variables and static methods directly. Static
methods can’t access instance methods and instance variables directly. They
must use reference to object. And static method can’t use this keyword as there
is no instance for ‘this’ to refer to.

this keyword :

Keyword 'this' in Java is a reference variable that refers to the current object.
"this" is a reference to the current object, whose method is being called upon.
You can use "this" keyword to avoid naming conflicts in the
method/constructor of your instance/object.

Page 2
COMPUTER APPLICATIONS Class X
TOPIC :- Methods( Lesson 23)

Page 3
COMPUTER APPLICATIONS Class X
TOPIC :- Methods( Lesson 23)

Example:

class Account{
int a;
int b;

public void setData(int a ,int b){


a = a; //this.a=a;
b = b; //this.b=b;
}
public void showData(){
System.out.println("Value of A ="+a);
System.out.println("Value of B ="+b);
}
public static void main(String args[]){
Account obj = new Account();
obj.setData(2,3);
obj.showData();
}
}

Page 4

You might also like