0
public class BankAccount {
  private static final double annualInterestRate = 1.5;
  private static double accountBalance = 150;
  private static double MonthlyInterest;


  public static void main(String[] args) {
    // TODO Auto-generated method stub
    calculateMonthlyInterest();   
    System.out.println(MonthlyInterest);
  }

  public static double calculateMonthlyInterest() 
  {
    double MonthlyInterest = (accountBalance * annualInterestRate);
    return MonthlyInterest;

  }
}

This is my code, I'm trying to return MonthlyInterest but when I print it out I get 0 instead of 225

Joeri Hendrickx
  • 16,947
  • 4
  • 41
  • 53
  • 6
    You've shadowed `MonthlyInterest` in `calculateMonthlyInterest`. You may need to remove the declaration of the local variable (just have `MonthlyInterest = (accountBalance * annualInterestRate);` as the body of the method) – ernest_k Feb 20 '19 at 15:07
  • 4
    and properly name variables: `monthlyInterest` and don't make everything `static`. – luk2302 Feb 20 '19 at 15:08
  • return (accountBalance * annualInterestRate); (if you don't need to assign it to a var) – aran Feb 20 '19 at 15:09
  • You have two MonthlyInterests declared. The first one at the top has class level scope, and the second one has method level scope. See https://stackoverflow.com/questions/30559515/difference-between-declaring-an-object-on-class-level-and-in-methods – togise Feb 20 '19 at 15:12

7 Answers7

2

The Problem

You're mixing static and non-static functionality in some pretty odd ways. Here's what's happening in your code:

private static double MonthlyInterest;

You create a static variable on your class. Since it's a primitive double, it defaults to storing 0. Also, variable names should start with a lower-case letter. This should be monthlyInterest. It won't break anything, but it's a standard Java convention to distinguish class names from variable names.

calculateMonthlyInterest();

You call the calculateMonthlyInterest() method.

double MonthlyInterest = (accountBalance * annualInterestRate);
return MonthlyInterest;

You calculate the monthly interest, store it in a new variable, and return it. Note that your static MonthlyInterest variable is not updated by this code. Instead, the local variable that you created "shadows" the static variable, and takes precedent over it.

calculateMonthlyInterest();
System.out.println(MonthlyInterest)

Your throw away your return value having never used it, and then print out the static MonthlyInterest that was never changed from its initial value of zero.

Here are two different approaches that you can take to make your code work:


With static variables

public class BankAccount {
    private static final double annualInterestRate = 1.5;
    private static double accountBalance = 150;
    private static double monthlyInterest;

    public static void main(String[] args) {
        calculateMonthlyInterest();
        System.out.println(monthlyInterest);
    }

    public static void calculateMonthlyInterest() {
        monthlyInterest = (accountBalance * annualInterestRate);
    }
}

With local variables

public class BankAccount {
    private static final double annualInterestRate = 1.5;
    private static double accountBalance = 150;

    public static void main(String[] args) {
        double monthlyInterest = calculateMonthlyInterest();
        System.out.println(monthlyInterest);
    }

    public static double calculateMonthlyInterest() {
        return accountBalance * annualInterestRate;
    }
}
Tom
  • 16,842
  • 17
  • 45
  • 54
Jordan
  • 2,273
  • 9
  • 16
0
public class BankAccount {
private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
    // TODO Auto-generated method stub
    calculateMonthlyInterest();


    System.out.println(MonthlyInterest);
}

public static void calculateMonthlyInterest() 
{
    MonthlyInterest = accountBalance * annualInterestRate ;


}


}
nullPointer
  • 4,419
  • 1
  • 15
  • 27
0

You don't need to return any value since you are keeping it in a static variable. If you want your method to actually return a value, assign it directly to MonthlyInterest. You also don't need to declare MonthlyInterest inside your method, since then the value is assigned to the local variable, and the printline takes value from the class static variable.

private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
    // TODO Auto-generated method stub
    MonthlyInterest = calculateMonthlyInterest();

    System.out.println(MonthlyInterest);
}

public static double calculateMonthlyInterest() 
{
    return  (accountBalance * annualInterestRate);
}
Ivan Kukic
  • 425
  • 4
  • 14
0
public static void main(String[] args) {
  double d = calculateMonthlyInterest();
  System.out.println(d);
}

Does this work?

Dominique
  • 16,450
  • 15
  • 56
  • 112
0
public static double calculateMonthlyInterest()
{
    MonthlyInterest = (accountBalance * annualInterestRate);
    return MonthlyInterest;
}

try this

0

You are creating a new double called 'MonthyInterest' inside your calculateMonthlyInterest method and setting your calculated value to that, instead of your static variable. You could fix this one of two ways: either don't create a new MonthlyInterest variable or return the value as you are now, and use that returned value in your main method (and delete the static MonthlyInterest variable). The code for both of those would look like this

public class BankAccount {
private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
    // TODO Auto-generated method stub
    calculateMonthlyInterest();


    System.out.println(MonthlyInterest);
}

public static void calculateMonthlyInterest() 
{
    this.MonthlyInterest = (this.accountBalance * this.annualInterestRate);

}


}

or

public class BankAccount {
private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;


public static void main(String[] args) {
    // TODO Auto-generated method stub
    double MonthlyInterest = calculateMonthlyInterest();


    System.out.println(MonthlyInterest);
}

public static double calculateMonthlyInterest() 
{
    return this.accountBalance * this.annualInterestRate;

}


}
wearebob
  • 376
  • 2
  • 15
0

You are defining MonthlyInterest 2 times:

  1. On the BankAccount class
  2. On the calculateMonthlyInterest method

these are two different variables with the same name becouse of the varable scope the one that you are assigning (accountBalance * annualInterestRate) is the one local to the calculateMonthlyInterest method so this doesn't affect the one defined on te BankAccount class

You can fix this assigning the returned value of calculateMonthlyInterest to the MonthlyInterest on the class scope :

private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
  MonthlyInterest = calculateMonthlyInterest();
  System.out.println(MonthlyInterest);
}

public static double calculateMonthlyInterest() {
  return  (accountBalance * annualInterestRate);
}

or not defining MonthlyInterest on the method:

private static final double annualInterestRate = 1.5;
private static double accountBalance = 150;
private static double MonthlyInterest;


public static void main(String[] args) {
  calculateMonthlyInterest();
  System.out.println(MonthlyInterest);
}

public static double calculateMonthlyInterest() {
  MonthlyInterest = (accountBalance * annualInterestRate);
}