• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Please explain why the following can't be marked static

 
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Please explain why the following can't be marked static:

1. interfaces
2. method local inner class
3. inner class methods and instance variables
4. local variables

Also why inner classes can be declared static and not the outer classes.


Thanks in advance
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Indra, anything preceeded by a static modifier belongs to a class. So, there has to be a top level class to which that particular component, code is assigned.

Interfaces are used to define the skeleton of a class. So, there is no point in accessing it except for proxy, be it an instance or static way of accessing.

Method local inner class, local variables are declared inside a method. Hence, their scope is just within the method and their access to the encircling class is very limited.

Instance variables are funny because if you add a 'static' modifier to it, it is no longer an instance variable..
 
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

indra negi wrote: ... Also why inner classes can be declared static and not the outer classes ...



Top level inner classes are members of the enclosing class hence can be declared as static (so they become static inner classes).
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Thanks Ravi and Vijitha for clearing the points.

Please explain why the inner class methods can't be marked as static.
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

indra negi wrote:Please explain why the inner class methods can't be marked as static.



For static methods to be accessed you don't need an instance of the class. That's why non static inner classes cannot have static methods.
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I understood that for static methods we don't need an instance of the class however it can still be invoked with an instance of the class. And if it can then why non static classes cannot contain static methods.

Please explain me this and also the basic difference between invoking a static method with an instance and without an instance. What happens when we declare a class as static?

Thanks in advance
 
Vijitha Kumara
Bartender
Posts: 4116
72
Mac TypeScript Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

indra negi wrote:I understood that for static methods we don't need an instance of the class however it can still be invoked with an instance of the class. And if it can then why non static classes cannot contain static methods.


If that allowed you are breaking a rule there (ability to invoke the static method without an instance).

Please explain me this and also the basic difference between invoking a static method with an instance and without an instance.


It's just the way you invoke the method. However it's not a good practice to use a reference to an instance to invoke static, you should always try to use the classname/interfacename (for static variables).


 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Got it. Thanks
 
Ranch Hand
Posts: 808
1
Android Eclipse IDE Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Vijitha Kumara wrote:

indra negi wrote:I understood that for static methods we don't need an instance of the class however it can still be invoked with an instance of the class. And if it can then why non static classes cannot contain static methods.


If that allowed you are breaking a rule there (ability to invoke the static method without an instance).

Please explain me this and also the basic difference between invoking a static method with an instance and without an instance.


It's just the way you invoke the method. However it's not a good practice to use a reference to an instance to invoke static, you should always try to use the classname/interfacename (for static variables).


In many cases there is no difference between invoking static method using class name and the reference name. But there is one exception. You can not instantiate static inner class through reference - only through the name of the outer class.
 
indra negi
Ranch Hand
Posts: 68
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thanks Lukas.

I understood that.
 
reply
    Bookmark Topic Watch Topic
  • New Topic