If the class members declared public and the class itself is specified as default access
( Package access), then can the public members of that class be used in another package?

I am asking this because .. as far i know class which is not specified as public cannot be accessed outside the its package . But the method :
public static void main()
is accessible even from outside the package eventhough i dont specify the class in which the main() is placed as public.
How is it possible.

If public methods can be accessed from anywhere then what is advantage of using access specifier to class itself?

Have you tried writing some classes to test this?

Mr. crash

U got me wrong...
I didnt mention static as a specifier...

Let me first clear u what my doubt is..

i mention a class without any access modifier which means it can be accessed only within the package.

now i define a static method in that class and make the access specifier of the static method is public, Then it is possible to access the static method even outside the package.
How is this possible.

package access;
class sample
{
public static void main(String[] args)
{
  // code
}
}

Now the main method will be called during execution ... and the call wil be from outside the package in which the class is defined and since i havent specified the acces level of class as public , how is the access possible?

Mr. Norm

I have tried this.

for example .. look at this

package access;
class s1
{
public static display()
{
System.out.println("this is a static method");
}
}//end of class s1

now consider a different package

package ss;
import access.*;

class sample
{
public static void main(String[] args)
{
s1.display();
}
}

The output will be :

this is a static method

Deleted

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.