*
The moose likes Beginning Java and the fly likes static methods Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "static methods " Watch "static methods " New topic
Author

static methods

Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
i didn't understand the statement "static methods cannot be overriden but can be redefined"....and how can it be done??
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Hi Mohnish and welcome to JavaRanch.
Have you studied static methods?
What training resources have you used so far?

Jim ... ...


BEE MBA PMP SCJP-6
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2991
    
    9
I recommend you check out OverridingVsHiding. Hiding a method means the same thing as redefining it, in this context.
Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65
please check page number 151 in scjp6 of kathy sierra book....in chapter 2 under heading accessing static methods and variables
Jim Hoglund
Ranch Hand

Joined: Jan 09, 2008
Posts: 525
Mohnish : What is your question? If you want ranchers to respond, it is important to
ask a clear question and show that you have tried to find an answer on your own.
For example, have you searched the JavaRanch site for relevant information?

Jim ... ...
Robby Robinson
Greenhorn

Joined: May 22, 2010
Posts: 6
Static mean it remains the same instances

for example when constantly open documents and they get doc doc1 doc2 doc3... so this a result of static variable .

now imagine if the main method was not static . you could hijack the entire program
Mohnish Khiani
Ranch Hand

Joined: May 17, 2010
Posts: 65


It's output is :
Static func1 of hobbit
Static func1 of hobbit_exp
Static func1 of hobbit

I understand that this is not dynamic polymorphism because which static method is to be invoked depends on the reference type rather than the actual object type....but then what is this called when I can redifine the static method in my subclass....do static methods get inherted?
Hunter McMillen
Ranch Hand

Joined: Mar 13, 2009
Posts: 492

Static methods belong to a class.

So if class A has a static method


And a Class B extends A, it inherits all properties of A


So running class B gives this output:
A

"If the facts don't fit the theory, get new facts" --Albert Einstein
abhilash pallerlamudi
Greenhorn

Joined: Jan 06, 2008
Posts: 4
Mohnish Khiani wrote:

It's output is :
Static func1 of hobbit
Static func1 of hobbit_exp
Static func1 of hobbit

I understand that this is not dynamic polymorphism because which static method is to be invoked depends on the reference type rather than the actual object type....but then what is this called when I can redifine the static method in my subclass....do static methods get inherted?



static methods does not participate in inheritance. if you redefine the behaviour in subclass, it is called as HIDING. Only instance methods participate in inheritance and can be overridden the behaviour.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2991
    
    9
abhilash pallerlamudi wrote:static methods does not participate in inheritance.

I disagree with this statement. Where did this idea come from? Do you have a link? If you look at, for example, the Java Tutorial on Inheritance, they don't say anything about static members not participating in inheritance. I think perhaps you mean to say that static methods do not participate in overriding. This is correct. But overriding is not the same as inheritance. It's just a different way that inheritance works for static members.
abhilash pallerlamudi
Greenhorn

Joined: Jan 06, 2008
Posts: 4
Mike Simmons wrote:
abhilash pallerlamudi wrote:static methods does not participate in inheritance.

I disagree with this statement. Where did this idea come from? Do you have a link? If you look at, for example, the Java Tutorial on Inheritance, they don't say anything about static members not participating in inheritance. I think perhaps you mean to say that static methods do not participate in overriding. This is correct. But overriding is not the same as inheritance. It's just a different way that inheritance works for static members.


AFAIK.. Static Members are accessed using Class name rather than inheriting them resulting in tight coupling. If static members are redefined in subclass, separate memory is allocated in their own namespace and does not encourage in code reusability thus voiding the Inheritance usage.
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2991
    
    9
So, no link? No idea where this information comes from? The way you use the term "inheritance" does not appear to have anything to do with how it's used in the Java Tutorial or Java Language Specification.
abhilash pallerlamudi
Greenhorn

Joined: Jan 06, 2008
Posts: 4
Mike Simmons wrote:So, no link? No idea where this information comes from? The way you use the term "inheritance" does not appear to have anything to do with how it's used in the Java Tutorial or Java Language Specification.


are statics inherited?
Mike Simmons
Ranch Hand

Joined: Mar 05, 2008
Posts: 2991
    
    9
Did you notice that not everyone there agrees on the answer to the question? At least one person in that conversation is wrong. And I would bet it is not Peter den Haan. In any event I would think that the Java Tutorial and Language Specification are more authoritative here. I already linked to the tutorial, did you read it? Nothing about statics not being inherited there. And in the JLS, "inherited" is defined in JLS 6.4.3:
Members are either declared in the type, or inherited because they are accessible members of a superclass or superinterface which are neither private nor hidden nor overridden(§8.4.8).

The members of a class type are all of the following:

Members inherited from its direct superclass (§8.1.4), if it has one (the class Object has no direct superclass)
Members inherited from any direct superinterfaces (§8.1.5)
Members declared in the body of the class (§8.1.6)

Again, nothing about static. Being hidden can prevent a method being inherited, but so can being overridden. If an accessible method in a superclass is not overridden or hidden, then it's inherited. Regardless of whether it's static or not.
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 37958
    
  22
abhilash pallerlamudi wrote: . . . are statics inherited?
See if you can find another link; that discussion is really difficult to understand. We have an FAQ, for example.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: static methods
 
Similar Threads
Static methods can be overridden by static methods only?
fun with main() and abstract
method overriding
native methods
Static methods - overriding