Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Example of an instance method? (Java)

I'm still learning about methods in Java and was wondering how exactly you might use an instance method. I was thinking about something like this:

public void example(String random) {
}

However, I'm not sure if this is actually an instance method or some other type of method. Could someone help me out?

like image 568
user2446065 Avatar asked Jun 10 '13 22:06

user2446065


1 Answers

If it's not a static method then it's an instance method. It's either one or the other. So yes, your method,

public void example(String random) {
  // this doesn't appear to do anything
}

is an example of an instance method.

Regarding

and was wondering how exactly you might use an instance method

You would create an instance of the class, an object, and then call the instance method on the instance. i.e.,

public class Foo {
   public void bar() {
      System.out.println("I'm an instance method");
   }
}

which could be used like:

Foo foo = new Foo(); // create an instance
foo.bar(); // call method on it
like image 180
Hovercraft Full Of Eels Avatar answered Oct 03 '22 06:10

Hovercraft Full Of Eels