Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / November 2007

Tip: Looking for answers? Try searching our database.

void method()

Thread view: 
Crouchez - 07 Nov 2007 11:10 GMT
Is a plain "void method()" public, protected or private by default?
Esmond Pitt - 07 Nov 2007 11:20 GMT
> Is a plain "void method()" public, protected or private by default?

No.
Patricia Shanahan - 07 Nov 2007 11:22 GMT
> Is a plain "void method()" public, protected or private by default?

No. Unfortunately, Sun chose not to allow use of a keyword for the
fourth access mode, "default access", which is really package access.
http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36154

Patricia
Crouchez - 07 Nov 2007 11:26 GMT
> > Is a plain "void method()" public, protected or private by default?
>
> No. Unfortunately, Sun chose not to allow use of a keyword for the
> fourth access mode, "default access", which is really package access.

http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36154

> Patricia

so it's basically private?
Esmond Pitt - 07 Nov 2007 11:31 GMT
>>>Is a plain "void method()" public, protected or private by default?
>>
[quoted text clipped - 6 lines]
>
> so it's basically private?

No, it is 'none of the above'. It means 'accessible to all classes
within the same package'. As it says in the post you quoted and the link
cited therein.
Crouchez - 07 Nov 2007 11:46 GMT
> >>>Is a plain "void method()" public, protected or private by default?
> >>
> >>No. Unfortunately, Sun chose not to allow use of a keyword for the
> >>fourth access mode, "default access", which is really package access.

http://java.sun.com/docs/books/jls/second_edition/html/names.doc.html#36154

> >>Patricia
> >
[quoted text clipped - 3 lines]
> within the same package'. As it says in the post you quoted and the link
> cited therein.

So what's this ThreadGroup method available to?:

/**
    * Adds the specified Thread to this group.
    * @param t the Thread to be added
    * @exception IllegalThreadStateException If the Thread group has been
destroyed.
    */
   void add(Thread t) {
    synchronized (this) {
       if (destroyed) {
        throw new IllegalThreadStateException();
       }
       if (threads == null) {
        threads = new Thread[4];
       } else if (nthreads == threads.length) {
        Thread newthreads[] = new Thread[nthreads * 2];
        System.arraycopy(threads, 0, newthreads, 0, nthreads);
        threads = newthreads;
       }
       threads[nthreads] = t;

       // This is done last so it doesn't matter in case the
       // thread is killed
       nthreads++;
           nUnstartedThreads--;
    }
   }
Lothar Kimmeringer - 07 Nov 2007 11:55 GMT
>> No, it is 'none of the above'. It means 'accessible to all classes
>> within the same package'. As it says in the post you quoted and the link
>> cited therein.
>
> So what's this ThreadGroup method available to?:

Do you read any of the answers you were receiving so far? The answer
to the question is in the text you cited directly before asking. Again:

>> It means 'accessible to all classes within the same package'.

If you don't know what package ThreadGroup is in: If it's not
specified explicitly as import-statement and you simply have
a ThreadGroup in your code, it's java.lang.

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

Patricia Shanahan - 07 Nov 2007 11:59 GMT
>>> Is a plain "void method()" public, protected or private by default?
>>>
[quoted text clipped - 5 lines]
>
> so it's basically private?

No, private is more restricted than default. A private method can only
be accessed inside the body of the top level class that encloses its
declaration.

Patricia
Lothar Kimmeringer - 07 Nov 2007 18:16 GMT
> A private method can only
> be accessed inside the body of the top level class that encloses its
> declaration.

... and its inner classes. Or is that what you mean with "inside
the body"?

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

Roedy Green - 07 Nov 2007 19:11 GMT
On Wed, 7 Nov 2007 19:16:32 +0100, Lothar Kimmeringer
<news200709@kimmeringer.de> wrote, quoted or indirectly quoted someone
who said :

>... and its inner classes. Or is that what you mean with "inside
>the body"?

they get complicated. See
http://mindprod.com/jgloss/nestedclasses.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Patricia Shanahan - 07 Nov 2007 19:26 GMT
>> A private method can only
>> be accessed inside the body of the top level class that encloses its
>> declaration.
>
> ... and its inner classes. Or is that what you mean with "inside
> the body"?

The exact statement in the JLS is: "Otherwise, if the member or
constructor is declared private, then access is permitted if and only if
it occurs within the body of the top level class (§7.6) that encloses
the declaration of the member or constructor."

I believe that is intended to include inner classes declarations.

Patricia
Roedy Green - 07 Nov 2007 19:09 GMT
>so it's basically private?

no. It is visible to all classes in the package.  See
http://mindprod.com/jgloss/scope.html

Private is visible only to the current class.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 07 Nov 2007 19:10 GMT
>so it's basically private?

see http://mindprod.com/jgloss/gettingstarted.html

You need at least one text book, perhaps out of date, and hence very
cheap, to teach you the basics of the language syntax.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Owen Jacobson - 08 Nov 2007 02:37 GMT
> > > Is a plain "void method()" public, protected or private by default?
>
[quoted text clipped - 4 lines]
>
> so it's basically private?

No; default ("package private", as mentioned elsewhere) access exists
independently of private access.  There's a useful progression of
access levels:

'private' allows access from within the same top-level class.
default allows access from anywhere private allows access, as well as
to any other class in the same package.
'protected' allows access from anywhere default allows access, as well
as from any subclass.
'public' allows access from anywhere protected allows access, as well
as from anywhere else.  (Ok, that one's a little contrived.)

For the java.lang.ThreadGroup class, other classes in java.lang are
allowed to manipulate its default-access members; since you can only
add classes to this package via vendor-specific trickery (and you
shouldn't do that), you can treat it as an implementation detail and
pretend it doesn't exist.  On the other hand, you can use default
access in your own packages to allow closely-related classes (like a
container and its iterator) to directly access one another's members,
if that's the cleanest way to implement something.
Are Nybakk - 07 Nov 2007 12:03 GMT
> Is a plain "void method()" public, protected or private by default?

The default is called "package private", that is, it is only accessible
within a package.

Private allow only the one class to access, protected allow the class
and it's subclasses access and last, public allow any class to access.

Hope it was understandable =)
Crouchez - 07 Nov 2007 13:13 GMT
> The default is called "package private", that is, it is only accessible
> within a package.

"package private". got it. cheers
Lothar Kimmeringer - 07 Nov 2007 18:18 GMT
> Private allow only the one class to access,

... and its inner classes

> protected allow the class
> and it's subclasses access

... and include package visible as well. There is no protected
as you know it e.g. with C++.

> and last, public allow any class to access.

At least that's correct ;-)

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

Crouchez - 07 Nov 2007 13:06 GMT
So if a plain method (ie. void method()) is part of a class in the Sun JDK
it's basically private?
Lew - 07 Nov 2007 14:20 GMT
> So if a plain method (ie. void method()) is part of a class in the Sun JDK
> it's basically private?

There is no "basically private" access.  There are four access levels, and a
method or member's access is exactly one of the four, never "basically" any of
them.

If a method (as in your example) is declared without any access modifier, then
it has exactly package-private access.  Not private.  Private access means
accessible only to the class itself.  Package-private is more exposed than
that; in addition, any class in the same package has access to it.

Signature

Lew

Crouchez - 07 Nov 2007 14:58 GMT
> > So if a plain method (ie. void method()) is part of a class in the Sun JDK
> > it's basically private?
[quoted text clipped - 10 lines]
> --
> Lew

I know but what I am saying is if you write a 3rd party app and create a new
java.lang.ThreadGroup you can't use it's method "void add(Thread t)"
lyallex - 07 Nov 2007 16:36 GMT
>>> So if a plain method (ie. void method()) is part of a class in the Sun
> JDK
[quoted text clipped - 16 lines]
> I know but what I am saying is if you write a 3rd party app and create a new
> java.lang.ThreadGroup you can't use it's method "void add(Thread t)"

void add(Thread t) is a method in ThreadGroup that has default (or
package private) access. This mean that classes in java.lang can access
this method (because it is package private) however as you are unable to
 'add' classes to the java.lang package you can't access this method
from your third party app. Actually in Eclipse you can write and compile
a class in the java.lang package but if you try to run it you get
java.lang.SecurityException: Prohibited package name: java.lang which is
interesting... gosh, it's been a while since I did the security thing.
Owen Jacobson - 07 Nov 2007 20:14 GMT
> >>> So if a plain method (ie. void method()) is part of a class in the Sun
> > JDK
[quoted text clipped - 25 lines]
> java.lang.SecurityException: Prohibited package name: java.lang which is
> interesting... gosh, it's been a while since I did the security thing.

Classes in packages starting with the string "java" can only be loaded
by the bootstrap classloader, which on Sun's JVM gets its classpath
from the -Xbootclasspath parameter (right now; subject to change
whenever Sun feels like it) and from the JVM's installation path.

This allows, for example, containers to provide implementations of
javax.servlet via the bootstrap classpath, even though that class is
neither part of the JRE library nor allowable from the default
classloader or its other children.
Lothar Kimmeringer - 07 Nov 2007 18:23 GMT
> I know but what I am saying is if you write a 3rd party app and create a new
> java.lang.ThreadGroup you can't use it's method "void add(Thread t)"

You would be able to if your class is also in the package
java.lang. But because the java.lang-package is special and
the compiler won't let you compile classes with that package
"you" (as long as you're not working on the Java-compiler) are
not able to access that method.

If you write a new mypackage.ThreadGroup-class that derives from
java.lang.ThreadGroup (I'm not sure if ThreadGroup isn't final
but that would make no difference here) you can't access that
method, either because it's only package visble and not protected
or public.

To go back to your problem. Why do you want to access that method
at all?

Regards, Lothar
Signature

Lothar Kimmeringer                E-Mail: spamfang@kimmeringer.de
              PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

Always remember: The answer is forty-two, there can only be wrong
                questions!

Roedy Green - 07 Nov 2007 19:12 GMT
>So if a plain method (ie. void method()) is part of a class in the Sun JDK
>it's basically private?

No. It is visible to all the classes in the package.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 07 Nov 2007 19:08 GMT
>Is a plain "void method()" public, protected or private by default?

it is "default" by default.  Unfortunately there is no keyword to
explicitly state you want the "default" scope, and not official name
for the default scope other than "default".

It is a bit like saying you bought a "default-coloured" car, but the
car manufacturer refuses to refer to the colour by any name other than
"default" in its literature.

It is sometimes called "package" scope, but you can't use the package
keyword.

see http://mindprod.com/jgloss/scope.html
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Roedy Green - 07 Nov 2007 19:17 GMT
>Is a plain "void method()" public, protected or private by default?

Your question implies you have big hole in your knowledge.  It will
take a long time to fill it by asking individual questions on a
newsgroup.  Instead get a text book and read up on scope. Then ask
questions about what still does not make sense.

You can also read my more informal essays on scope.
Start at http://mindprod.com/jgloss/scope.html
and follow the links.

I recall wrestling with this long ago, doing experiments, and finally
deciding it was all trivial and obvious.  Oddly, I have yet to figure
out a way to explain it all that makes its obviousness immediately
apparent.

Have faith that you will have some aha moments and you will wonder
what all the fuss as about.

Anonymous classes are another matter.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Crouchez - 07 Nov 2007 21:35 GMT
> >Is a plain "void method()" public, protected or private by default?
>
[quoted text clipped - 20 lines]
> The Java Glossary
> http://mindprod.com

Well the package private is barely ever used and I'd say 99% of methods have
a named access modifier
Roedy Green - 07 Nov 2007 23:18 GMT
>Well the package private is barely ever used and I'd say 99% of methods have
>a named access modifier

I think you are overgeneralising.

Consider a large but simple class like java.lang.String:
I counted 78 public,  4 default, 2 private..

Consider a more complicated class like java.util.Pattern, you see a
quite different distribution:
163 default, 69 private, 9 public.

Consider a  simple standalone 1-class application, designed to do
work, rather than provide services to client:
8 private, 1 public.

Default is common when you have a complicated problem with mulitple
classes and lots of interaction between classes.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Crouchez - 08 Nov 2007 09:46 GMT
I've just released what package private actually is - it's the old basic
method (like in many old procedural languages) that didn't include method
security - limited to the working folder.
Roedy Green - 08 Nov 2007 20:08 GMT
>I've just released what package private actually is - it's the old basic
>method (like in many old procedural languages) that didn't include method
>security - limited to the working folder.

Learning Java is much like learning a foreign language. The sooner you
start learning to think in the language, rather than think in your
native language and translating, the better command of the language
you develop. This is even truer of Java than most other computer
languages because Java is so strongly idiomatic.  There are canned
ways of doing almost everything.  You build very little from scratch
from the primitive elements.
Signature

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com

Adam Maass - 08 Nov 2007 08:26 GMT
> Is a plain "void method()" public, protected or private by default?

None of the above. It is 'default.' Its visibility is restricted to classes
in the current package, and subclasses in other packages.

-- Adam Maass
Lew - 08 Nov 2007 14:40 GMT
"Crouchez" <safdfasdf@asdfasdfs.com> wrote:
>> Is a plain "void method()" public, protected or private by default?

> None of the above. It is 'default.'

True.

> Its visibility is restricted to  classes in the current package,

True.  (Although "its own" would be a better adjective than "current".)

>  and subclasses in other packages.

False.  That would require protected access.

Signature

Lew



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.