Example of an instance method? (Java)

后端 未结 5 2005
伪装坚强ぢ
伪装坚强ぢ 2020-12-30 11:08

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 vo         


        
5条回答
  •  野趣味
    野趣味 (楼主)
    2020-12-30 11:30

    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
    

提交回复
热议问题