Results 1 to 5 of 5
  1. #1
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Why can't inner classes have static Methods?

    For example, if I want to count the number of instances of an Inner class, a static method in the inner class would be suitable for that, right?

    But I am wondering why can't we have static methods in an inner class?

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

    Default Re: Why can't inner classes have static Methods?

    An inner class only exists for a particular object of the outer class; think of an instantiation of an inner class as a planet; a planet only exists for a particular star (an object of the outer class). What seems to be a static member of the inner class, simply is a member of the object of the outer class.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  3. #3
    Join Date
    May 2016
    Location
    Brazil
    Posts
    16
    Rep Power
    0

    Default Re: Why can't inner classes have static Methods?

    @Jos: Are you suggesting using the outer class state/behavior to do this instance count?

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

    Default Re: Why can't inner classes have static Methods?

    Quote Originally Posted by Nathan Rodriguez View Post
    @Jos: Are you suggesting using the outer class state/behavior to do this instance count?
    Yep; what seems to be 'static' w.r..t. that inner class only exists in that particular instance of the outer class.

    kind regards,

    Jos
    Build a wall around Donald Trump; I'll pay for it.

  5. #5
    Zarah is offline Senior Member
    Join Date
    Mar 2014
    Posts
    286
    Rep Power
    7

    Default Re: Why can't inner classes have static Methods?

    @JosAH Got it, thank you very much.

Similar Threads

  1. Replies: 1
    Last Post: 07-12-2012, 11:04 PM
  2. Replies: 2
    Last Post: 07-12-2012, 11:04 PM
  3. Replies: 8
    Last Post: 04-16-2012, 11:00 PM
  4. Static methods or individual classes?
    By nik_meback in forum AWT / Swing
    Replies: 6
    Last Post: 12-01-2010, 03:52 PM
  5. Call Static Methods Outside Classes
    By Insomniac Riot in forum New To Java
    Replies: 4
    Last Post: 05-11-2010, 10:03 PM

Posting Permissions

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