Accessibility

" Accessibility Domain of a member should never be larger than that of the class containing it. If such thing happens, Accessibility of member is reduced to that of the class"

I read this in a book

class A

{

publicstaticvoid main(String args[]){}

}

here, A has default scope, and main has public scope

So main's scope should have had been reduced to a default scope

Now, from my knowledge, i suspect public is used in main so, that when i m using my interpretor (java A), it should be able to access main from outside program.

but since main has been reduced to default scope, no1 can access main outside my program, but still the code compiles ?

[967 byte] By [enterneoa] at [2007-11-27 10:10:27]
# 1

> [i]

> " Accessibility Domain of a member should never be

> larger than that of the class containing it. If such

> thing happens, Accessibility of member is reduced to

> that of the class"

> [/i]

>

> I read this in a book

> [code]

> class A

> {

>public static void main(String args[]){}

> de]

>

> here, A has default scope, and main has public scope

>

> So main's scope should have had been reduced to a

> default scope

Well, you certainly can't access it outside that package through A, since A isn't visible outside that package. However, if the method were not static, and if A implemented an interface that defined that method, then you could access that method through an interface (or public superclass) reference.

> but since main has been reduced to default scope, no1

> can access main outside my program, but still the

> code compiles ?

Why wouldn't it compile?

jverda at 2007-7-13 0:47:20 > top of Java-index,Java Essentials,New To Java...
# 2

as far as i know

main method need to be public in order for the interpretor to execute my main method outside the package

now since A has default scope, so main has also been reduced to default scope and is no longer public, so how is my interpretor still able to call it from outside the package?

enterneoa at 2007-7-13 0:47:20 > top of Java-index,Java Essentials,New To Java...
# 3

> as far as i know

>

> main method need to be public in order for the

> interpretor to execute my main method outside the

> package

But you can still create a non-public main method. You just won't be able to use it as the entry point for the VM.

Although I think, contrary to that rule, the VM may be able to invoke a public main on a non-public class.

As far as the compiler is concerned, however, main is just another method. Nothing special.

jverda at 2007-7-13 0:47:20 > top of Java-index,Java Essentials,New To Java...
# 4

> As far as the compiler is concerned, however,

> main is just another method. Nothing special.

agreed!

> But you can still create a non-public main method.

> You just won't be able to use it as the entry point

> for the VM.

agreed !

> Although I think, contrary to that rule, the VM may

> be able to invoke a public main on a non-public

> class.

ooooh!, please elucidate

also i tried this

class Demo

{

static void main(String args[])

{

}

}

java Demo, complains that main is not public, this confirms that main needs to be public

but if class is default, and main is public, still main would finally be default and not public, so still java Demo should complain instead of compiling

Message was edited by:

enterneo

enterneoa at 2007-7-13 0:47:20 > top of Java-index,Java Essentials,New To Java...
# 5

> > Although I think, contrary to that rule, the VM

> may

> > be able to invoke a public main on a non-public

> > class.

> ooooh!, please elucidate

Non-public MyClass. Public main. java MyClass may or may not still work. I thought I saw it work, but I'm not sure. It's meaningless either way, as far as I'm concerned, but if you're interested, try it.

jverda at 2007-7-13 0:47:20 > top of Java-index,Java Essentials,New To Java...