visibility of a class

Hi ,i have a public static void main(String[] args){}And the class contaning this method has default visibility , then how is it that my code gets executed . How is the code acessible from outsideregards,ivin
[257 byte] By [ivinjacoba] at [2007-11-27 5:40:33]
# 1
Any class(In class path) is accessible from JVM. when we invoke java interpreter on a class, It searches for a method with signature 'void main(String argv[])' if present, it starts executing else it displays message 'In class NoMain: void main(String argv[]) is not defined'
Satish_Patila at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 2
then why does it not work for protected or for private?
ivinjacoba at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 3
If you try to compile a class with private or protected modifier. you will get Compiler error. A class can have only public(or default) modifier.
Satish_Patila at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 4
As per the concept , a default visibility is such that only classes in that package should be able to access .How how can we accomodate this concept , in this scenario , where the java interpretor is able to access a method in a class that is declared as default
ivinjacoba at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 5
Is there any diffenerence beteween declaring a class as public and default?expecting a reply
ivinjacoba at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 6
there is defference b/w class accessing other class and Interpreter accessing class.
Satish_Patila at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 7
> Is there any diffenerence beteween declaring a class> as public and default?Yes(In terms of Accessibility)
Satish_Patila at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 8
i have clear idea of decalring variables and methods as private,protected ,default and publicbut i am not getting the idea of declaring the class as default and public since .in anycase the class is accessible by the interpretor also
ivinjacoba at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 9

Are you actually using various packages? It seems to me you are working with one class which is package visible and you don't even have it in a package other than a default package, this will run fine. Imagine you have this class in another package and then call it from a class in another package, then it will not work. You will understand this and find it more useful as your programs grow.

_helloWorld_a at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 10

> i have clear idea of decalring variables and methods

> as private,protected ,default and public

>

> but i am not getting the idea of declaring the class

> as default and public since .

>

> in anycase the class is accessible by the interpretor

> also

Do you know what a package is?

_helloWorld_a at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 11
We can ristrict a class accessing other class. But, Interpreter has access to everything.
Satish_Patila at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 12
thnx for the reply
ivinjacoba at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 13
yesdefault - can access in same package or different package class but not accessible in different package sub-class.but public - can access any where same and different package class and sub-class.
S_Singha at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 14

> If you try to compile a class with private or

> protected modifier. you will get Compiler error. A

> class can have only public(or default) modifier.

no,

but when u compile a class should be public because it is accessible any where, but private and protected not accessible.

S_Singha at 2007-7-12 15:17:03 > top of Java-index,Java Essentials,New To Java...
# 15
> but when u compile a class should be public because> it is accessible any where, but private and protected> not accessible.It depends if you want it to be accessible or not. (Classes with a main method must be accessible).
totua at 2007-7-21 21:32:45 > top of Java-index,Java Essentials,New To Java...