GeeCON Prague 2014*
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static method modifier question 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 "Static method modifier question" Watch "Static method modifier question" New topic
Author

Static method modifier question

Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Found this in a practice exam and quite confused.



The answer is 'private'.

But my confusion is, this will work just fine without the private modifier and I cannot understand what is added by using the private modifier.
Without the modifier, the method is sup class will be hidden when a reference of type test is used to invoke the method.
Because static methods are always invoked from reference type, I'm not sure what can be achieved by using the private modifier.

Thank you.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Sorry.

Found this in the Inquisition question set (John Meyer).

Thank you.
Ankitt Gupta
Ranch Hand

Joined: Feb 19, 2009
Posts: 101
Static method cannot be overridden but can be redefine.This is an example of redefining.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thank you.
But I am wondering what difference it would make by adding the 'private' modifier when accessing those methods by their respective references?
Arka Guhathakurta
Ranch Hand

Joined: Mar 01, 2009
Posts: 46
Hi,
I would like to raise two issues with reference to your question.
1)Static methods cannot be overriden. You can easily find out the truth by adding @Override annotation before the function "method()" in the derived class.
2)Classes as sup and test cannot be declared as private or protected. Note that this type of class definitions may only be default or public.
The question is not clear to me. Adding final keyword before the class sup ensures that class sup cannot be inherited thus preventing override. I hope it clears your doubt.


regards,
Arka
Guilherme Bazilio
Greenhorn

Joined: Mar 02, 2009
Posts: 23
Arka Guhathakurta wrote:
2)Classes as sup and test cannot be declared as private or protected. Note that this type of class definitions may only be default or public.
The question is not clear to me. Adding final keyword before the class sup ensures that class sup cannot be inherited thus preventing override. I hope it clears your doubt.


That's right.
But when we add the final modifier to the class, although it prevent other classes from extending it, a compile error would be thrown. So, Maduranga Liyanage, it does not answer your question.

And I think the private access modifier cannot be used for classes.

So I really don't mind what would be the answer. What is that?

Thanks!
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thank you for the replies.

My question is not regarding classes. It is about the modifiers of static methods.

Basically, static methods are invoked on reference type.
Suppose I have two classes Parent and Child (extending Parent) and both have static methods by the same name. Static methods are not overridden but hidden. So when I invoke the static method from a Parent reference, I can invoke the Parent class method and vice versa.

On the other hand, 'private' modifier hides (static or non-static) the method from the subclasses. And there is no way the subclass can access the super class method (static or non-static).

The question asks:What class modifier(s) can be inserted at line 1 to prevent method() from being overridden (hidden ) without causing compile time errors ?

I am not sure what the question is trying to ask exactly. Static methods are 'hidden' when they are overridden in the subclass. You dont need the private modifier to do that. The only reason you would add private modifier imo is to prevent the subclass from invoking it. So I think the problem is, what is the question really asking for?
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
the thinng is only that,
the ""private"" modifier completely hide the metthod() in class sup outside world including your inheriting class have no idea
even what private method it have.
even if its not static same applies no normal methods.
class him
{
private int fun()
{
}
}

class kim extends him
{
int fun() //no prob.....if you make this even private then in next extending class you can use the same method
{
}
}


SCJP 5
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thank you Himanshu.. that is what I wanted to know..
himanshu kesarwani
Ranch Hand

Joined: Aug 27, 2008
Posts: 84
your welcome....!! and best of luck
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Though there is the question as to why one would need a private access modifier on a static method, as static methods cannot be overridden. If a static member is visible as we know it can only be called through a reference or instance to the class of which it belongs. However if it were to be marked private and static, this would be a function of such a method, not reachable outside the class which it belongs?




So in the above cases, the only argument I see for a private static method, is to ensure no use of the method outside its own class.
Sun suggests here that such a design may be used as an alternative to
static initialization blocks and that one advantage is that they can be reused later if you need to reinitialize a class variable.
This also, in my opinion, at least therefore echoes to me elements of the Singleton design pattern. Is this correct, or am I way off?


be a well encapsulated person, don't expose your privates, unless you public void getWife()!
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thank you very much for the clarification Stephen..
But what I gather is that the private modifier does not add anything special whether it is used in a static method or not, am I right?
Stephen Davies
Ranch Hand

Joined: Jul 23, 2008
Posts: 352
Well it does do something, in regards to static modifiers. It prevents any other class using the method, even if statically called. Whilst we may question its effectiveness in this sense, in the sense of static initialization and (in my assumption) the Singleton method, it is indeed effective. So be wary of thinking it does not do something it certainly does.

In a non-static usage private is a very important modifier, and it certainly does something, so do not be mistaken. One of the private modifer's most important functions is to facilitate encapsulation, which as you may or may not know, is a vital design principle in the Java programming world. Without the private modifier, things would be sticky to say the least.

I think you should read a little more about access modifiers, data-hiding and encapsulation, this will help you grasp the situation a little more.
sara adrem
Greenhorn

Joined: Jul 26, 2007
Posts: 15
>>>>> But what I gather is that the private modifier does not add anything special whether it is used in a static method or not, am I right?

It does make a difference. if you declare a method as private, that method can be called only within the code of that particular class

Eg...



<a href="http://eathumblepie.blogspot.com" target="_blank" rel="nofollow">http://eathumblepie.blogspot.com</a>
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Thank you very much Stephen and Sara.

When I said private modifier doesnt add anything special, I meant it doesnt add anything special when it is used in a static method in contrast to a non-static method.
For example, the 'protected' modifier has different behavior for static and non-static method. The protected methods can only accessed through a subclass instance if the class is in a different package if the method is non-static. But need not so when the method is static. That is what I meant by that the private modifier doesn't add anything special between the static and non-static methods.
It seems for me the behavior is the same whether private modifier is used on a non-static or a static method. am I wrong ?

Thank you very much.
Guilherme Bazilio
Greenhorn

Joined: Mar 02, 2009
Posts: 23
I think I got what you meant, Maduranga Liyanage.

Let's use sara adrem's example (keeping it's comments):



If staticMethod() cannot be accessed outside the class because it's private, so why does it needs to be declared static? What if it was a non-static method?

I got confused too. ;P

Thanks!
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
Yes. In this context, it doesn;t matter if the method was static or not. The behavior is the same.
Guilherme Bazilio
Greenhorn

Joined: Mar 02, 2009
Posts: 23

Maduranga Liyanage wrote:
Yes. In this context, it doesn;t matter if the method was static or not. The behavior is the same.


Thanks Liyanage. I think I got it now.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
You\re welcome Bazilo.
But there some differences when you use the protected modifier. The behavior is different for static and non-static methods. should make that clear too.
Guilherme Bazilio
Greenhorn

Joined: Mar 02, 2009
Posts: 23
Ok.. I think I got it clear.

The protected modifier I can imagine some uses as it isnĀ“t so restrict like private. :P

Thanks again!
Mahalakshmi Chandru
Ranch Hand

Joined: Feb 26, 2009
Posts: 48

The protected methods can only accessed through a subclass instance if the class is in a different package if the method is non-static. But need not so when the method is static.


Maduranga,
I read the whole thread on this topic. Its little bit confusing when thinking about the combination of 'static' and 'protected'. We can access non static protected methods in subclasses outside the package by using the instance. But how do we access the static protected methods in the subclass outside the package. Could you please explain the scenario with a sample code. Thanks.
Maduranga Liyanage
Ranch Hand

Joined: May 25, 2005
Posts: 124
I'm in a bit of a hurry so I will give a code I have tried later.
But basically, if I remember correctly, for protected static methods to be accessed outside the package you must extend the super class. And I am not quite sure whether you can access a non-static method by using a superclass instance. I think it must be a subclass instance. In the case of the static methods, you still must extend the class, but you can access the super class static method via both superclass and subclass instances. I will confirm this later.
Mahalakshmi Chandru
Ranch Hand

Joined: Feb 26, 2009
Posts: 48
Maduranga,
Thanks for your response. I can understand the scenario well now. Here is my understanding with a code sample:

Accessing protected non-static member


Also, when i tried to access the protected non static method using the superclass instance, it threw an error saying that the method is not visible.

Accessing protected static member



In order to access protected static member, we need to extend the super class in the subclass.Otherwise the protected member wont be visible.

Also, we will be able to access the superclass protected member by using both the super and subclass instances. But the actual way to access the static method , can be we can access the method using the class reference itself.

This explains the discussed scenario. Do you know where this situation happens in realtime.





 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static method modifier question