default

If we do not mention any access modifier, our Java class automatically thinks it has a default access modifier. If it is default, that means you can access this method anywhere in your package. But if you go out of this package, then you cannot access this method. Even if we import the package.classname into our new package, we will not be able to access this method if we have not specified it as public. If you don't specify it, then by default it thinks it's a default access modifier. The default access modifier can be accessed anywhere in the package, but not outside it.

In the Packages section, we imported this package and we tried to use it. As you can see in the following screenshot, there is an error showing up in line 15:

Quick fixes drop down with suggestions to correct the code error for default

If we don't specify anything, we cannot access it, therefore it is the same as that of the default feature. This applies to variables as well:

public class arrayListexample {
// can accept duplicate values
//ArrayList, LinkedList, vector- Implementing List interface
//array has fixed size where arraylist can grow dynamically
//you can access and insert any value in any index
int i=5;

As we can see, in the preceding code we have declared an integer. However, it will not be public; it is default. Thus, we cannot access the variable outside the package. If we import it, we will have access to the class but not the methods. If we want access, we have to write it as a public. So what does a public access modifier do?

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

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