void method()

E

Esmond Pitt

Crouchez said:
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.
 
C

Crouchez

Esmond Pitt said:
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?:

/**
* 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--;
}
}
 
L

Lothar Kimmeringer

Crouchez said:
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:

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
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Patricia Shanahan

Crouchez said:
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
 
A

Are Nybakk

Crouchez said:
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 =)
 
C

Crouchez

So if a plain method (ie. void method()) is part of a class in the Sun JDK
it's basically private?
 
L

Lew

Crouchez said:
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.
 
C

Crouchez

Lew said:
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.

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)"
 
L

lyallex

Crouchez said:
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.
 
L

Lothar Kimmeringer

Patricia said:
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
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Lothar Kimmeringer

Are said:
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
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Lothar Kimmeringer

Crouchez said:
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
--
Lothar Kimmeringer E-Mail: (e-mail address removed)
PGP-encrypted mails preferred (Key-ID: 0x8BC3CD81)

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

Roedy Green

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,577
Members
45,054
Latest member
LucyCarper

Latest Threads

Top