How can i private instance variable of a class can be accessed by its object? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

How can i private instance variable of a class can be accessed by its object?

private variables cannot be accessed by any other class and objects ..right? https://code.sololearn.com/czfaKE3MZFSr/?ref=app

25th Jun 2017, 8:08 AM
suryapoojary
suryapoojary - avatar
5 Answers
+ 1
You use getter and setter methods
25th Jun 2017, 8:27 AM
Da' BO$
Da' BO$ - avatar
+ 1
like: public String getName(){ return private_Name; }
25th Jun 2017, 9:29 AM
Limitless
Limitless - avatar
+ 1
@Limitless ..hehhe..i got that buddy..but im trying to understand the importance of private access modifier
25th Jun 2017, 11:29 AM
suryapoojary
suryapoojary - avatar
25th Jun 2017, 11:26 AM
Limitless
Limitless - avatar
0
When you want to get for example your IP address from your computer, the class that controls it will only allow you to GET it, and not SET it. It is there to help the whole program work as it should. Instead of allowing the Programmer to change their IP address, it only allows them to view it. I hope you understand the example. There are other examples as well. Like get current time.
25th Jun 2017, 11:38 AM
Limitless
Limitless - avatar