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 / January 2006

Tip: Looking for answers? Try searching our database.

Local Classes - Access Modifiers?

Thread view: 
duff - 15 Jan 2006 00:01 GMT
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)
Roedy Green - 15 Jan 2006 00:17 GMT
>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.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

duff - 15 Jan 2006 01:19 GMT
>>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.
Tony Morris - 14 Jan 2006 17:28 GMT
> >>http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
> >>(scroll to the end)
[quoted text clipped - 4 lines]
>
> 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/
Roedy Green - 15 Jan 2006 09:22 GMT
>> >>http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
>> >>(scroll to the end)
[quoted text clipped - 6 lines]
>
>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
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Java custom programming, consulting and coaching.

Stefan Schulz - 15 Jan 2006 12:16 GMT
> >>http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
> >>(scroll to the end)
[quoted text clipped - 4 lines]
>
> 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();
   }
}
duff - 16 Jan 2006 10:11 GMT
>> >>http://java.sun.com/docs/books/tutorial/java/javaOO/innerclasses.html
>> >>(scroll to the end)
[quoted text clipped - 7 lines]
>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;
[quoted text clipped - 21 lines]
>    }
>}
Stefan Schulz - 16 Jan 2006 12:10 GMT
> >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.
  */
}
Daniel Dyer - 16 Jan 2006 14:00 GMT
>> >Also note that the StackIterator class is not a local class, but an
>>
[quoted text clipped - 4 lines]
> 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.

Signature

Daniel Dyer
http://www.dandyer.co.uk

Stefan Schulz - 16 Jan 2006 14:49 GMT
> 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.
Mike Schilling - 15 Jan 2006 16:40 GMT
> 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
[quoted text clipped - 6 lines]
> 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.


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.