Java: Accessing Private Variables in Client Class

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

The discussion clarifies that in Java, private instance variables can be modified through public methods, even when accessed from a client class. The example provided involves a class named Auto with a private variable gas and a public method addGas(int g). When an instance of Auto is created and addGas is called from outside the class, the private variable gas is successfully updated. This demonstrates the encapsulation principle in object-oriented programming.

PREREQUISITES
  • Understanding of Java access modifiers (public, private)
  • Familiarity with object-oriented programming concepts
  • Basic knowledge of Java class and method structure
  • Experience with Java instance variables and constructors
NEXT STEPS
  • Explore Java encapsulation and its importance in software design
  • Learn about Java constructors and their role in object initialization
  • Investigate Java method overloading and overriding
  • Study best practices for designing public APIs in Java classes
USEFUL FOR

Java developers, software engineers, and students learning object-oriented programming principles will benefit from this discussion, particularly those interested in understanding encapsulation and access control in Java.

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
774
  • · 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