Java: Accessing Private Variables in Client Class

  • Context: Java 
  • Thread starter Thread starter Bleys
  • Start date Start date
  • Tags Tags
    Java
Click For Summary

Discussion Overview

The discussion revolves around the accessibility of private instance variables in a Java class when accessed from a client class. It examines whether a public method in a supplier class can modify a private variable when called from outside that class.

Discussion Character

  • Technical explanation
  • Conceptual clarification

Main Points Raised

  • One participant questions whether calling a public method from a client class will change a private variable in the supplier class.
  • Another participant confirms that the private variable can be modified through the public method, emphasizing that direct access to the private variable is not possible.
  • A code example is provided to illustrate the scenario, showing the instantiation of the class and the method call.

Areas of Agreement / Disagreement

Participants generally agree that the public method can modify the private variable, but the discussion does not explore any competing views or unresolved issues.

Contextual Notes

No limitations or unresolved issues are noted in the discussion.

Who May Find This Useful

Java programmers, especially those interested in object-oriented programming concepts related to access modifiers.

Bleys
Messages
74
Reaction score
0
Hello there; I have a very short question about client class access:

Suppose in a supplier class an instance variable is declared private (and is used in the constructor), and a void method in the same class (declared public) changes this variable. In the client class, is calling this method still going to change the variable (assuming an object of that class is instantiated of course). I know if the method is called within its own class then the variable will be changed, no problem. I was wondering whether the fact it's being called outside of its class makes a difference.
Something like:

public class Auto(){
private int gas;

public Auto(){
gas = 0
}

public void addGas(int g){
gas = gas + g;
}
}

and if addGas is called somewhere outside of the class, will the gas variable still be accessed and changed?
 
Technology news on Phys.org
Bleys said:
Hello there; I have a very short question about client class access:

Suppose in a supplier class an instance variable is declared private (and is used in the constructor), and a void method in the same class (declared public) changes this variable. In the client class, is calling this method still going to change the variable (assuming an object of that class is instantiated of course). I know if the method is called within its own class then the variable will be changed, no problem. I was wondering whether the fact it's being called outside of its class makes a difference.
Something like:

public class Auto(){
private int gas;

public Auto(){
gas = 0
}

public void addGas(int g){
gas = gas + g;
}
}

and if addGas is called somewhere outside of the class, will the gas variable still be accessed and changed?
Yes, but it will not be accessed directly. Your public method addGas will do that.

Code:
Auto car = new Auto();
car.addGas(10);

The car instance is created with its gas property initialized to 0. The next line calls the addGas method to add 10 (gallons, liters?) of gas.
 
thank you, Mark!
 

Similar threads

  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 34 ·
2
Replies
34
Views
4K
  • · Replies 4 ·
Replies
4
Views
1K
  • · Replies 3 ·
Replies
3
Views
1K
  • · Replies 36 ·
2
Replies
36
Views
3K
  • · Replies 36 ·
2
Replies
36
Views
6K
  • · Replies 13 ·
Replies
13
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 1 ·
Replies
1
Views
4K