aspose file tools*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Please explain why the following can't be marked static Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Please explain why the following can Watch "Please explain why the following can New topic
Author

Please explain why the following can't be marked static

indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68
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
Karthick Ravi
Greenhorn

Joined: Feb 27, 2008
Posts: 13
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..
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

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


SCJP 5 | SCWCD 5
[How to ask questions] [Twitter]
indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68

Thanks Ravi and Vijitha for clearing the points.

Please explain why the inner class methods can't be marked as static.
Vijitha Kumara
Bartender

Joined: Mar 24, 2008
Posts: 3825

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

Joined: Aug 12, 2009
Posts: 68
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

Joined: Mar 24, 2008
Posts: 3825

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

Joined: Aug 12, 2009
Posts: 68

Got it. Thanks
Lucas Smith
Ranch Hand

Joined: Apr 20, 2009
Posts: 804
    
    1

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.


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
indra negi
Ranch Hand

Joined: Aug 12, 2009
Posts: 68
Thanks Lukas.

I understood that.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Please explain why the following can't be marked static