File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes Static methods and variables. Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "Static methods and variables." Watch "Static methods and variables." New topic
Author

Static methods and variables.

Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Que 1) How does overloading and overriding work in case of static methods and variables ?
Que 2) Can Private methods be overloaded and overriden? What is "shadowing" of methods? Somebody used this term while we were discussing some java issues, is this a valid term and if yes then what does it mean ?
------------------
http://www.mantrotech.com


----------------------<br />regards.<br /><a href="http://www.mantrotech.com/editors/gaurav_mantro.asp" target="_blank" rel="nofollow">Gaurav Mantro</a><br /><a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">Java Information Portal</a> - <a href="http://www.mantrotech.com/technology/java/index.asp" target="_blank" rel="nofollow">http://java.mantrotech.com</a>
Pragya Prakash
Ranch Hand

Joined: Sep 18, 2000
Posts: 42
Answer to question no.2
Private methods can be overloaded, but not overridden.
Methods cannot be overridden to have less accessibility.
For eg:- we can override a public method to be private, but not vice versa.
Regards
Sean Casey
Ranch Hand

Joined: Dec 16, 2000
Posts: 625
Answer to question 1
You can overload static methods since an overloaded method is really a different method with the same name. But since a method is static (belonging to the class and not to a particular instance) you cannot override it.
Rakesh Sharma
Ranch Hand

Joined: Dec 29, 2000
Posts: 37
if a method is static than overridden method should also be static. Thats what I read somewhere. Is that correct?
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Yes static method can be overriden by another static method only.
Look at the following code
class Base
{
static void PrintString()
{
System.out.println("Base Static Method");
}
}
public class Test extends Base
{
public static void main(String argv[])
{
Test objTest = new Test();
Base objBase = objTest;
}
void PrintString()
{
System.out.println("Test Static Method");
}
}

It gave me following compiler error
ERROR:
The instance method void PrintString() declared in class Test cannot override the static method of the same signature declared in class Base. It is illegal to override a static method.
Last sentence in Error message seems confusing as static methods can be overridden as long as overriding method is also static.
------------------
http://www.mantrotech.com
bill bozeman
Ranch Hand

Joined: Jun 30, 2000
Posts: 1070
Gaurav, that is not true. You cannot override a static method. If you try to override it with a non-static method, you get a compile time error. If you try to override it with another static method, it compiles and runs fine, but it is not overriding. It is hiding.
The best way to understand this is to think what happens when you override a method. If you have a Parent class name A and a child class that extends A named B, then you create an instance like this:
A a = new B();
perfectly legal and used all the time. Then when you call a method like a.aMethod();, if it is an overridden method the body from class B() runs because of late-binding. But with a static method, you get don't get the method body of B() but of A() because it is not overriding the method, it is hiding it.
Try it out with a sample and see what you get.
Bill
Velmurugan Periasamy
Ranch Hand

Joined: Nov 09, 2000
Posts: 95
Static methods are not overridden. They are shadowed or hidden. Static methods are resolved at compile time based on the type of the reference variable(which class it is?) and are not subject to late binding(which object it is?).
Private methods are also not overridden, since they are not inherited.
HTH
------------------
Velmurugan Periasamy
Sun Certified Java Programmer
----------------------
Study notes for Sun Java Certification
http://www.geocities.com/velmurugan_p/


------------------------------------------------------------------------------------<BR>Velmurugan Periasamy<BR>Sun Certified Java Programmer for Java 2 Platform<BR>Sun Certified Web Component Developer for J2EE platform<BR>Sybase Certified EAServer Developer (similar to jCert level 3)<BR>------------------------------------------------------------------------------------<BR>Study notes for Sun Java Certification<BR><A HREF="http://www.geocities.com/velmurugan_p/" TARGET=_blank rel="nofollow">http://www.geocities.com/velmurugan_p/</A><BR>------------------------------------------------------------------------------------
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
Static methods that are overridden in the derived class do not exhibit polymorphic behavior. The compiler will allow it, but at runtime you cannot invoke the override method in the derived class using a reference from the base class.

Output is:
Base.foo()
Derived.bar()
-Peter

[This message has been edited by Peter Tran (edited February 07, 2001).]
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
Originally posted by bill bozeman:

The best way to understand this is to think what happens when you override a method. If you have a Parent class name A and a child class that extends A named B, then you create an instance like this:
A a = new B();
perfectly legal and used all the time. Then when you call a method like a.aMethod();, if it is an overridden method the body from class B() runs because of late-binding. But with a static method, you get don't get the method body of B() but of A() because it is not overriding the method, it is hiding it.

Hi Bill!
Theoratically I agree to you and that's what I think too. But I don't have enough information to prove the same. I had a debate with a good friend of mine that static methods can't be overridden. Can you provide information in terms of object layout in memory (regd static and non static methods and what happens when we override a method). I know little bit about virtual tables in C++, how does java handle that ?
The following code supports the fact that static methods can't be overridden

Output of the above code
Test Static Method
Base Static Method
Test Method
Test Method
best regards
Gaurav Mantro

[This message has been edited by Gaurav Mantro (edited February 07, 2001).]
[This message has been edited by Gaurav Mantro (edited February 07, 2001).]
Peter Tran
Bartender

Joined: Jan 02, 2001
Posts: 783
From the JLS:

8.4.6.1 Overriding (by Instance Methods)
An instance method m1 declared in a class C overrides another method with the same signature, m2, declared in class A if both
1. C is a subclass of A.
2. Either
* m2 is non-private and accessible from C, or
* m1 overrides a method m3, m3 distinct from m1, m3 distinct from m2, such that m3 overrides m2.

What the JLS does say about static methods with the same signature in both Base and Derived class is:

8.4.6.2 Hiding (by Class Methods)
If a class declares a static method, then the declaration of that method is said to hide any and all methods with the same signature in the superclasses and superinterfaces of the class that would otherwise be accessible to code in the class. A compile-time error occurs if a static method hides an instance method.

-Peter
[This message has been edited by Peter Tran (edited February 07, 2001).]
Golam Newaz
Ranch Hand

Joined: Jan 08, 2001
Posts: 64
Hi,
I complied Gaurav's program and it is ok that static method is not overridden but what about static variable. Look the following example and run it and static variable is overridden.
I don't understand why this is the difference because both of them are class variable and class method.
class Testthis{
boolean avariable;
static public int i=5;
Testthis(){}; // Constructor 1.

Testthis(boolean avariable, int i){ // Constructor 2.
this.avariable=avariable;
this.i=i; // assigning to the member class/static variable i.

}
}

public class Testoverride extends Testthis{
public static void main( String argv[] ){
Testthis th=new Testthis( true,10 ); // used constructor
// no.2 of superclass.
System.out.println(th.avariable); // avariable is an
// instance variable
// of class Testthis
// super class
System.out.println(th.i);
System.out.println(Testthis.i);

}
}
Output is:
true
10
10
- Golam Newaz

------------------
Cherry Mathew
Ranch Hand

Joined: Dec 26, 2000
Posts: 159
Hi golam ,
I couldnt find any overriding in your code.
u r getting the same output in both the calls because u r assigning 10 to the variable in the constructor.
I didnt get u r doubt
Cherry
Gaurav Mantro
Ranch Hand

Joined: Sep 08, 2000
Posts: 61
I think, Golam you need to modify your code to test overriding for variables.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Static methods and variables.
 
Similar Threads
why i=i++; doesn't get incremented
Inner classes question
nested classes
Java questions
Constructor and Initializer block