Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8854677
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T13:56:21+00:00 2026-06-14T13:56:21+00:00

I have a class in java for methods. Basically this class receives an array

  • 0

I have a class in java for methods. Basically this class receives an array of integer numbers, adds the numbers & subtracts them too, returns the sum and the subtraction. I declared the variables at the top (not in a particular method). When the subtraction and the addition are done they’re assigned to their respective variables (automatically, of course), BUT when the method is finished doing its job the values are deleted, so when I call a method of the subtraction/addition the result is a 0.

As far as I know the values shouldn’t be empty because they’re not initialized inside a method but outside all methods, so the scope shouldn’t have ended. Any help, please ?

//Class of the methods

    package chap3;

    import javax.swing.JOptionPane;

    /**
    *
    * @author jtech
    */
    public class SimpleArithmeticMethods
    {
   int sum;
   int subtraction;

public void sum_Difference(int[] nums)
{        
    int[] inpNums = nums; 

    sum = inpNums[0] + inpNums[1];
    subtraction = inpNums[1] - inpNums[0]; 

}

public void getSum()
{
     JOptionPane.showMessageDialog(null,"The sum is: "+sum, "Result.", JOptionPane.INFORMATION_MESSAGE);
}

public void getDifference()
{
    JOptionPane.showMessageDialog(null,"The difference is: "+subtraction, "Result.", JOptionPane.INFORMATION_MESSAGE);
}

}    

The class from which I run

 package chap3;

 import javax.swing.JOptionPane;

 /**
  *
 * @author jtech
 */
public class SimpleArithmetic 
{

  public static void main(String[] args)
  {
    String[] strInptNums = new String[2];
    int[] inptNums = new int[2];         

    SimpleArithmeticMethods obtainSum = new SimpleArithmeticMethods();
    SimpleArithmeticMethods obtainDifference = new SimpleArithmeticMethods();
    SimpleArithmeticMethods workSum_Difference = new SimpleArithmeticMethods();

    for (int counter = 0; counter <= 1; counter++)
    {
        strInptNums[counter] = JOptionPane.showInputDialog(null, "Input a number, smallest first", "Input Data.", JOptionPane.QUESTION_MESSAGE);
        inptNums[counter] = Integer.parseInt(strInptNums[counter]);
    }        

    workSum_Difference.sum_Difference(inptNums);
    obtainSum.getDifference(); 
    obtainDifference.getDifference();
  }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T13:56:22+00:00Added an answer on June 14, 2026 at 1:56 pm

    You’re calling the sum_Difference() method on one object, and display the results using another object.

    That’s like storing a message in a bottle, and then looking if the message is in another bottle. Use the same object to call all three methods.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a simple Java class that has some methods: public class Utils {
I have a class in java that reads UDP packets and puts them in
Basically, I have a class with 2 methods: one to serialize an object into
I have a problem with calling an overriden method from java class. I have
So in java, say you have a non-static method 'bar()' in an class 'Foo'.
I'm using Java. I want to have a setter method of one class that
In php have a class SoapClient I need a equivalente class in Java, anyone
like in java I have: Class.getSuperClass().getDeclaredFields() how I can know and set private field
I have a java class and I need to debug it (put breakpoints and
I have a java class containing all the columns of a database table as

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.