Java Java: Accessing Private Variables in Client Class

  • Thread starter Thread starter Bleys
  • Start date Start date
  • Tags Tags
    Java
Click For Summary
In the discussion, a user inquires about accessing and modifying a private instance variable in a supplier class from a client class. The example provided involves a class named Auto, which has a private variable 'gas' and a public method 'addGas' that modifies this variable. It is clarified that while the 'gas' variable is private and cannot be accessed directly from outside the class, it can still be modified through the public method 'addGas'. When an instance of Auto is created and 'addGas' is called, the method successfully updates the 'gas' variable, demonstrating that public methods can manipulate private variables even when called from outside the class.
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!
 
Learn If you want to write code for Python Machine learning, AI Statistics/data analysis Scientific research Web application servers Some microcontrollers JavaScript/Node JS/TypeScript Web sites Web application servers C# Games (Unity) Consumer applications (Windows) Business applications C++ Games (Unreal Engine) Operating systems, device drivers Microcontrollers/embedded systems Consumer applications (Linux) Some more tips: Do not learn C++ (or any other dialect of C) as a...

Similar threads

  • · Replies 0 ·
Replies
0
Views
614
  • · 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