Win a copy of Testing JavaScript Applications this week in the HTML Pages with CSS and JavaScript forum!
  • 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Bear Bibeault
  • Ron McLeod
  • Jeanne Boyarsky
  • Paul Clapham
Sheriffs:
  • Tim Cooke
  • Liutauras Vilda
  • Junilu Lacar
Saloon Keepers:
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • fred rosenberger
  • salvin francis
Bartenders:
  • Piet Souris
  • Frits Walraven
  • Carey Brown

why is an interface's varable static, and a method not allowed to be static?

 
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
just started wondering about this
any ideas?
 
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Interfaces may only declare static final field members (in short constants) and abstract methods that will have to be implemented in implemeting classes. Since abstract methods cannot be static, it is prohibited to declare interface methods to be static.
[ March 07, 2003: Message edited by: Valentin Crettaz ]
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
okay, abstract can't be static, cool. i was wondering why there was a discrepancy between variables being allowed and methods not.
that info helps, thankyou .
 
Valentin Crettaz
Author & Gold Digger
Posts: 7617
6
IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, don't hesitate to stick your nose in the Java Language Specification. You can spare lots of time once you know where to find that kind of information. Everything you ever need to know about the Java language is in there
 
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The book The Java Programming Language by Arnold, Gosling and Holmes provides quite a few explanations and insights into why the rules are the way they are, that is, the reasons for design decisions.
Here is what they say about static fields in interfaces:
�Because interfaces contain no implementation details, they cannot define normal fields--such a definition would be dictating implementation policy to the classes that choose to implement the interface. Interfaces can define named constants because these are useful in the design of types.�
Then they give an example to show what they mean. See 4.2.1 Interface Constants.
 
Jasper Vader
Ranch Hand
Posts: 284
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
thanks for the help. Marlene - that sounds like a good explanation.
valentin - i will start exploring the JLS .
 
Marlene Miller
Ranch Hand
Posts: 1392
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Here is an example from the Java API of how named constants declared in interfaces are �useful in the design of types�.
In the interface java.awt.image.ImageObserver, the method imageUpdate has a parameter called infoflags.

The infoflags argument should be the bitwise inclusive OR of the following flags: WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT.
The values WIDTH, HEIGHT, PROPERTIES, SOMEBITS, FRAMEBITS, ALLBITS, ERROR, ABORT are static final int fields declared in the interface.
 
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Dumb question but why can't abstract methods be static?
 
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shanel,
Abstract means they should be overridden, static means they cannot be overridden.
class A{
Method()
}

If you declare Method() as abstract then you have to override that method
in the subclass unless the subclass itself is abstract. with static you cannnot override it.
Thus both are opposite to each other and hence not allowed.
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joshua,

I only know Abstract and Final are opposites. Page 103 of K&B Java 1.4 said the same thing you told me "static methods can't be overridden!".

I still don't understand why Static can't be overriden though, do we have to assume that Static has characteristics of Final?

Thank you.
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shanel.
consider the following example

class Parent{
public void commonMethod()
{
System.out.println("parent class")
}
}

class Child{
public static void main(String[] args)
{
Parent p = new Child();
p.commonMethod();
}
public void commonMethod()
{
System.out.println("child class")
}
}

The output is parent class.
This shows that commonMethod() has not been overridden. Had it been the
out would have been child class.

Hope this helps
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shanel,
one more thing i forgot to mention in my previous post.

if you try to override final method, then you will get compile time error.
But not in case of static methods. Because although static methods cannot be overriden they can be hidden.
 
Shanel Jacob
Ranch Hand
Posts: 112
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi Joshua,

I copied your code and tried to compile. Was only able to compile it after adding "class Child extends Parent" and adding ";" at the end of the two System.out.println.

The output I got is: "child class" though. Can you explain?

Also, how does the code help explain "Static can't be overriden though"?

---------

class Parent
{
public void commonMethod()
{
System.out.println("parent class");
}
}

class Child extends Parent
{
public static void main(String[] args)
{
Parent p = new Child();
p.commonMethod();
}

public void commonMethod()
{
System.out.println("child class");
}
}
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi shanel I made the biggest blunder by not naming the commonMethod() to be static.
Make it static and see...
 
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i tried out the above code by adding the keyword static in method signatures of both the Parent and Child class It does not compile...and it shouldn't compile
 
joshua antony
Ranch Hand
Posts: 117
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well it compiles fine for me





javac Child.java
java Child


output: parent class
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
hey the code did compile......i got the o/p as parent class.....does it mean.(i dont want to make this confusing) that one can declare a static method in subclass, seemingly overrideing a static method in its superclass? But actually what happens is that the method does not get overriden.....is it?
 
Suhas Wadadekar
Ranch Hand
Posts: 97
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i actually tried making either of the methods static...in that case the does not compile......

so i reached a conclusion that
1. If a method is overridden...then both methods should not be static
2. If both the methods are static, then it is not an override.
3. Having two methods in superclass/subclass each with the same signature except for the static modifier does not allow the cose to compile.

Please let me know if my conclusions are flawed
 
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
"why is an interface's varable static, and a method not allowed to be static?"

One thing to make sure you understand is that a variable defined in an interface is not only static, but it is final, and therefore a constant.

That should help you understand the purpose of variables (not particularly variable though..) in interfaces.
 
    Bookmark Topic Watch Topic
  • New Topic