Important Notice: Our web hosting provider recently started charging us for additional visits, which was unexpected. In response, we're seeking donations. Depending on the situation, we may explore different monetization options for our Community and Expert Contributors. It's crucial to provide more returns for their expertise and offer more Expert Validated Answers or AI Validated Answers. Learn more about our hosting issue here.

What is the difference between an instance method and a class method?

class instance method
0
10 Posted

What is the difference between an instance method and a class method?

0
10

Ans : An instance method has a this instance reference, whereas a class method does not. An instance method always works hand in hand with a class instance. An instance method must be invoked on a specific class instance, and it has special access to the data in that instance. For example: String s1 = “abcde”; // one String instance String s2 = “fgh”; // another String instance int i1 = s1.length(); // i1 = 5 int i2 = s2.length(); // i2 = 3 The length method returns the number of characters in its target object (s1 or s2 in this example), just as if that target were an argument to a length function (cf. strlen(aString) in C). When defining an instance method, you can use the this keyword to refer to the target instance. Class methods have no target instance and thus no implicit this reference. Class methods can always be used, even if you have no instance of the class on hand. Class methods, like class variables, are declared with the static keyword. For example, the currentThread meth

Related Questions

What is your question?

*Sadly, we had to bring back ads too. Hopefully more targeted.

Experts123