private

If we mark our method or variable as private, then we cannot access either of them outside the class. They cannot be accessed outside the package, or outside the same class. If we want to access this in our ArraysDemo example, we cannot do that. Even if we try to, it throws an error, as shown in the following screenshot:

Quick fixes drop down with a suggestion to correct the code error for private

This is so because, if we mark any method or variable as private, we cannot access it outside that particular class. Until and unless we change it to something else, it throws an error. This applies to the variable as well.

If you want a real-time scenario, let's say you are making a payment and buying a product; all the credit card details will go as private because they will not be accessed outside of that buy class. If they can be accessed, that's a security breach, right? So to make credit card details restricted to that particular class, the developers give the private variable to all the card details so other classes cannot use it. Even if they have used inheritance or are importing a package, they will not have access to those sensitive details. There are a lot of real-time scenarios; if you are working on test frameworks, there might be some variables which you should not change, and always keep it private.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
3.141.31.209