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]

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'
then why does it not work for protected or for private?
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.
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
Is there any diffenerence beteween declaring a class as public and default?expecting a reply
there is defference b/w class accessing other class and Interpreter accessing class.
> Is there any diffenerence beteween declaring a class> as public and default?Yes(In terms of Accessibility)
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
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.
> 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?
We can ristrict a class accessing other class. But, Interpreter has access to everything.
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.
> 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.
> 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 >
