Java Java: Accessing Private Variables in Client Class

  • Thread starter Thread starter Bleys
  • Start date Start date
  • Tags Tags
    Java
AI Thread 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!
 
Thread 'Is this public key encryption?'
I've tried to intuit public key encryption but never quite managed. But this seems to wrap it up in a bow. This seems to be a very elegant way of transmitting a message publicly that only the sender and receiver can decipher. Is this how PKE works? No, it cant be. In the above case, the requester knows the target's "secret" key - because they have his ID, and therefore knows his birthdate.
Thread 'Project Documentation'
Trying to package up a small bank account manager project that I have been tempering on for a while. One that is certainly worth something to me. Although I have created methods to whip up quick documents with all fields and properties. I would like something better to reference in order to express the mechanical functions. It is unclear to me about any standardized format for code documentation that exists. I have tried object orientated diagrams with shapes to try and express the...

Similar threads

Replies
0
Views
369
Replies
34
Views
4K
Replies
3
Views
1K
Replies
36
Views
3K
Replies
13
Views
1K
Replies
1
Views
2K
Replies
1
Views
3K
Back
Top