Example of an instance method? (Java)

后端 未结 5 2130
伪装坚强ぢ
伪装坚强ぢ 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:46

    class InstanceMethod
        {
         public static void main(String [] args){
             InstanceMethod obj = new InstanceMethod();// because that method we wrote is instance we will write an object to call it
               System.out.println(obj.sum(3,2));
         }
         int f;
         public double sum(int x,int y){// this method is instance method because we dont write static
    
              f = x+y;
              return f;
          }
      }
    

提交回复
热议问题