Help coderanch get a
new server
by contributing to the fundraiser
  • 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
  • Ron McLeod
  • Paul Clapham
  • Devaka Cooray
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • paul wheaton
  • Henry Wong
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Tim Moores
  • Carey Brown
  • Mikalai Zaikin
Bartenders:
  • Lou Hamers
  • Piet Souris
  • Frits Walraven

static methods

 
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
i didn't understand the statement "static methods cannot be overriden but can be redefined"....and how can it be done??
 
Ranch Hand
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mohnish and welcome to JavaRanch.
Have you studied static methods?
What training resources have you used so far?

Jim ... ...
 
Master Rancher
Posts: 4971
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I recommend you check out OverridingVsHiding. Hiding a method means the same thing as redefining it, in this context.
 
Mohnish Khiani
Ranch Hand
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 525
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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 ... ...
 
Greenhorn
Posts: 6
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 65
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


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?
 
Ranch Hand
Posts: 492
Firefox Browser VI Editor Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 4971
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 4971
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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
Master Rancher
Posts: 4971
79
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Marshal
Posts: 79635
380
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic