Private variable is accessible from outside. Why?

I can’t understand why I still can assign a value to a private variable with dot notation when create an object.

public class CarLoan {

  int carLoan;

  int loanLength;

  private static int interestRate;

  int downPayment;

  CarLoan(int carLoan, int loanLength, int downPayment) {

    this.carLoan = carLoan;

    this.loanLength = loanLength;

    this.downPayment = downPayment;

  }

  public void setInterstRate(int rate) {

    this.interestRate = rate;

  }

  public void calculateLoan() {

    if (loanLength <= 0 || interestRate <= 0) {

    System.out.println("Error! You must take out a valid car loan.");

  } else if (downPayment >= carLoan) {

    System.out.println("The car can be paid in full.");

  } else {

    int remainingBalance = carLoan - downPayment;

    int months = loanLength * 12;

    int monthlyBalance = remainingBalance / months;

    int interest = monthlyBalance * interestRate / 100;

    int monthlyPayment = monthlyBalance + interest;

    System.out.println(monthlyPayment + "$");

  }

}

 

  public static void main(String[] args) {

    CarLoan loan1 = new CarLoan(5000, 30, 2000);

    loan1.setInterstRate(19);

    loan1.calculateLoan();

    CarLoan loan2 = new CarLoan(100000, 36, 20000);

    loan2.interestRate = 8;

    loan2.calculateLoan();

  }

}

As you can see my interstRate is private but I am able to assign a variable to it outside the object like that.

loan2.interestRate = 8;

Shouldn’t private modifier prevent from it?

You will need to use the setter method inside your class.

The setter method will basically be called like you have it

public void setInterestRate(int interestRate)
{
  this.interestRate = interestRate;
}

And then when you are creating an object using the setter method.

So loan2.setInterestRate(8);

Remember that when you are creating a constructor, you’d need setter and getter methods.

But yeah, due to the fact the variable is private, it is visible only inside that part. So you are unable to access it without a getter or setter method.

@elgrecoo , thank you for the reply. In my code I have setter inside my class don’t I? The problem is that I can still set

interestRate

outside the class, in main class, using dot notation, although it shouldn’t be the case.

public void setInterstRate(int rate) {

    this.interestRate = rate;

  }

There is no main class. That’s the main method. What you declared basically is a global variable used in public class CarLoan.

If you tried to create a different java file with a different class name and declare it there as private, then you couldn’t use it inside the class that has your main method directly.

Right now, your variable named interestRate is declared GLOBALLY as a private static int inside the public class Car Loan which contains the main method, thus, the main method can access that variable.

I hope you get what I mean :smiley: :smiley:

2 Likes

Ok, I understand now, thank you

1 Like