Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Java by (9.5k points)

I’ve the below code to implement instance method:

public void example(String random) {

}

Can anyone tell me the right way to code for instance methods and when to use it?

1 Answer

0 votes
by (19.7k points)

If you haven’t declared it as static, then the above code is an example of instance method. With this, you can create an instance of the class, an object, and then call the instance method on the instance like below: 

public class Foo {

   public void bar() {

      System.out.println("I'm an instance method");

   }

}

//calling the instance method

Foo foo = new Foo(); // create an instance

foo.bar(); // call method on it

Interested in Java? Check out this Java Certification by Intellipaat. 

Related questions

0 votes
1 answer
0 votes
1 answer
asked Mar 16, 2021 in Python by laddulakshana (16.4k points)
0 votes
1 answer
0 votes
0 answers
asked Jun 24, 2021 in Java by Harsh (1.5k points)

Browse Categories

...