Calling a Void Method

Currently I have this code coded as a part of a separate Java class.

 public void setSubtotal ()
 {
   subtotal = Validator.getDouble(sc,"Enter subtotal:", 0, 10000);    
 }

And I want to call to it from another method. I already have this class instantiated so I can call to it, but I'm not sure how to call to this method since it's a void method.

3

2 Answers

all you gotta do is

 this.setSubtotal();

since you're doing it inside the same class

13
public class Y{
   public static void main(String args[]){
       X foo = new X();
       foo.setSubtotal();
   }
}

public class X{
public void setSubtotal ()
 {
   subtotal = Validator.getDouble(sc,"Enter subtotal:", 0, 10000);    

 }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Maya Lin-Takahashi

Maya Lin-Takahashi

Consumer Tech & Gadget Reviewer

Maya is a hardware enthusiast who tests and reviews smart home devices, smartphones, wearables, and audio gear. She focuses on practical consumer value and build quality.

Share this article
Twitter Facebook Pinterest