Local Classes - Access Modifiers?

Discussion in 'Java' started by Tony Morris, Jan 14, 2006.

  1. Tony Morris

    Tony Morris Guest

    "duff" <> wrote in message
    news:...
    > On Sun, 15 Jan 2006 00:17:53 GMT, Roedy Green
    > <> wrote:
    >
    > >On Sun, 15 Jan 2006 13:01:47 +1300, duff <>
    > >wrote, quoted or indirectly quoted someone who said :
    > >
    > >>http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
    > >>(scroll to the end)

    > > 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.


    Not much can be said, but welcome to Sun documentation :)

    --
    Tony Morris
    http://tmorris.net/

    Java Questions and Answers
    http://jqa.tmorris.net/
    Tony Morris, Jan 14, 2006
    #1
    1. Advertising

  2. Tony Morris

    duff Guest

    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)
    duff, Jan 15, 2006
    #2
    1. Advertising

  3. Tony Morris

    Roedy Green Guest

    Roedy Green, Jan 15, 2006
    #3
  4. Tony Morris

    duff Guest

    On Sun, 15 Jan 2006 00:17:53 GMT, Roedy Green
    <> wrote:

    >On Sun, 15 Jan 2006 13:01:47 +1300, duff <>
    >wrote, quoted or indirectly quoted someone who said :
    >
    >>http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
    >>(scroll to the end)

    > 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.
    duff, Jan 15, 2006
    #4
  5. Tony Morris

    Roedy Green Guest

    On Sun, 15 Jan 2006 03:28:46 +1000, "Tony Morris" <>
    wrote, quoted or indirectly quoted someone who said :

    >> >>http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
    >> >>(scroll to the end)
    >> > 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.

    >
    >Not much can be said, but welcome to Sun documentation :)


    You can file a "bug" report in the Sun bug parade (images of marching
    ladybugs carrying parasols) to get the documentation fixed too.
    See http://mindprod.com/jgloss/bugs.html
    --
    Canadian Mind Products, Roedy Green.
    http://mindprod.com Java custom programming, consulting and coaching.
    Roedy Green, Jan 15, 2006
    #5
  6. duff wrote:

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

    > > 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.


    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();
    }
    }
    Stefan Schulz, Jan 15, 2006
    #6
  7. "duff" <> wrote in message
    news:...
    > 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.
    Mike Schilling, Jan 15, 2006
    #7
  8. Tony Morris

    duff Guest

    On 15 Jan 2006 04:16:43 -0800, "Stefan Schulz"
    <> wrote:

    >
    >duff wrote:
    >
    >> >>http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
    >> >>(scroll to the end)
    >> > 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.

    >
    >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.

    >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();
    > }
    >}
    duff, Jan 16, 2006
    #8
  9. duff wrote:

    > >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?


    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.
    */
    }
    Stefan Schulz, Jan 16, 2006
    #9
  10. Tony Morris

    Daniel Dyer Guest

    On Mon, 16 Jan 2006 12:10:13 -0000, Stefan Schulz
    <> wrote:

    > duff wrote:
    >
    >> >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?

    >
    > 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.


    --
    Daniel Dyer
    http://www.dandyer.co.uk
    Daniel Dyer, Jan 16, 2006
    #10
  11. Daniel Dyer wrote:

    > 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.
    Stefan Schulz, Jan 16, 2006
    #11
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. shila
    Replies:
    3
    Views:
    671
    Tor Iver Wilhelmsen
    Feb 24, 2004
  2. Razvan
    Replies:
    11
    Views:
    895
    Andrew Thompson
    Jul 17, 2004
  3. Indudhar
    Replies:
    4
    Views:
    364
    Patrick May
    Oct 21, 2005
  4. man4*.*

    access modifiers

    man4*.*, Nov 18, 2006, in forum: Java
    Replies:
    4
    Views:
    355
    man4*.*
    Nov 19, 2006
  5. Miquel

    Class & modifiers modifiers

    Miquel, Jan 25, 2007, in forum: Ruby
    Replies:
    4
    Views:
    160
    Miquel
    Jan 26, 2007
Loading...

Share This Page