Local Classes - Access Modifiers?

D

duff

have you tried compiling it or something similar? The tutorial might
have been written before the language spec was nailed down.

Compile time error with that example.

It must be quite recent as generics was used on that page.
 
S

Stefan Schulz

duff said:
Compile time error with that example.

It must be quite recent as generics was used on that page.

Did you replace the ... parts with appropriate "null" returns? If i do
that, it compiles fine for me.

Also note that the StackIterator class is not a local class, but an
inner class of Stack. If it where a local class the code would look
like this:

public class Stack {
private ArrayList<Object> items;

//code for Stack's methods and constructors
//not shown

public Iterator<Object> iterator() {
class StackIterator implements Iterator {
int currentItem = items.size() - 1;

public boolean hasNext() {
return false; // implement!
}

public ArrayList<Object> next() {
throw new NoSuchElementException // implement!
}

public void remove() {
throw new UnsupportedOperationException();
}
}
return new StackIterator();
}
}
 
M

Mike Schilling

duff said:
def: Local Classes - a class which is local to a block of code -
typically within a method - and is not a member of the enclosing
class.

ok, I read somewhere that local classes (like local variables) can't
have access modifiers? Is this correct? If yes then what's with this
example:

http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
(scroll to the end)

That's not a local class; it's an inner class. Local classes are defined
within method bodies and are visible only to the method that contains them,
so an access modifier makes no sense. An inner class (like this one) could
be visible to the world, if it's public and its containing class is public,
so limiting its access does make sense.
 
D

duff

Did you replace the ... parts with appropriate "null" returns? If i do
that, it compiles fine for me.

Yup, just like your example below did.
Also note that the StackIterator class is not a local class, but an

hmm... but StackIterator is in a method, doesn't that make it a local
class - ie. not a member of the enclosing class?
inner class of Stack. If it where a local class the code would look
like this:

Yup, all because of the missing "private" access modifier.
 
S

Stefan Schulz

duff said:
hmm... but StackIterator is in a method, doesn't that make it a local
class - ie. not a member of the enclosing class?

It seems you are confused about the types of classes. Let me try to
illustrate things a bit further. Paste the following into a file called
Example.java:

public class Example {
/* a public toplevel class */

private class Nested {
/* this is a nested class. It may have any access modifier */
}

public void foo() {
class Local {
/* this is a local class, and may not have any access
* modifiers
*/
}

new Object(){
/* this is an anonymous class, which can not have any access
* modifier (and not even a name)
*/
}
}
}

class AnotherToplevel {
/* this is another toplevel class.
* The only legal modifier is public, but only if the class is
* declared in a file matching its name.
*/
}
 
D

Daniel Dyer

It seems you are confused about the types of classes. Let me try to
illustrate things a bit further. Paste the following into a file called
Example.java:

He's not confused, the example is broken, it has a local class with a
private modifier and doesn't compile unless you remove it. You might not
be looking at the same bit he's talking about, it's the very last section
of code, just above "Other Facts about Nested Classes".

Dan.
 
S

Stefan Schulz

Daniel said:
He's not confused, the example is broken, it has a local class with a
private modifier and doesn't compile unless you remove it. You might not
be looking at the same bit he's talking about, it's the very last section
of code, just above "Other Facts about Nested Classes".

Dan.

The last paragraph is correct. The example before it, however... my
guess is they meant to put the "StackIterator" class outside the
method. Definitly worth fixing, though.
 

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,755
Messages
2,569,536
Members
45,019
Latest member
RoxannaSta

Latest Threads

Top