What if I create a private variable inside the main class, would that mean it'll be available in other classes? What do private variables even do? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 5

What if I create a private variable inside the main class, would that mean it'll be available in other classes? What do private variables even do?

27th Feb 2016, 11:30 AM
Pavitran
Pavitran - avatar
3 Answers
+ 6
Private variables are only accessible from the same class/object and not by other classes.
7th Mar 2016, 9:41 PM
Tigu
Tigu - avatar
+ 1
The private access modifier is accessible only within class. Simple example of private access modifier let'say we have created two classes A and Simple. A class contains private data member and private method. We are accessing these private members from outside the class, so there is compile time error. Note : we use private only with class members i.e. with instance variables and instance methods and not with main outer class. This is one of the Java beans convention followed by developers
15th Aug 2016, 1:12 PM
Abhishek C Prajapati
Abhishek C Prajapati - avatar
0
new answer
15th Aug 2016, 4:15 PM
2799352cs