Results 1 to 4 of 4
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Can non-static Inner Class have static fields ?

    From Thinking In Java:

    Fields and methods in ordinary inner classes can only be at the outer level of a class, so ordinary inner classes cannot have static data, static fields, or nested classes.
    But I can compile this without any errors:

    Java Code:
    public class Parcel5 
    {
    	class myInnerClas
    	{
    		String namee;
    		static final int trythis = 5;
    	}
    }
    So, is there something I am missing, or is this just a little mistake in the book ?

  2. #2
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can non-static Inner Class have static fields ?

    Remove the 'final' qualifier and see for yourself. It is not a mistake in the book.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: Can non-static Inner Class have static fields ?

    But a static final int is a static data ?

  4. #4
    JosAH's Avatar
    JosAH is offline Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,361
    Blog Entries
    7
    Rep Power
    20

    Default Re: Can non-static Inner Class have static fields ?

    Not really; it's a compiler constant (and it actually is a mistake in the language).

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

Similar Threads

  1. Replies: 4
    Last Post: 07-19-2011, 01:30 PM
  2. Static and non static class methods question
    By silverglade in forum New To Java
    Replies: 2
    Last Post: 05-14-2011, 10:10 PM
  3. Replies: 3
    Last Post: 03-24-2010, 12:24 AM
  4. Replies: 3
    Last Post: 02-09-2010, 05:22 AM
  5. Static fields
    By Eranga in forum Advanced Java
    Replies: 25
    Last Post: 08-29-2009, 11:06 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •