override main()

The file name is A.java

class A

{

publicfinalstaticvoid main(String args[])

{

System.out.println("in A");

A a =new B();

a.main(args);

}

}

class Bextends A

{

publicstaticvoid main(String args[])

{

System.out.println("in B");

}

}

i expect output to be

in A

in B

but I get stuck in a loop, that prints in A continously

[1085 byte] By [enterneoa] at [2007-11-27 9:37:58]
# 1
Static methods can't be overridden, and that's why you always should invoke static methods on the class.Kaj
kajbja at 2007-7-12 23:10:23 > top of Java-index,Java Essentials,New To Java...
# 2
I am wondering how r u able to compile the code... the compiler would clearly state "overridden method is static final"....
@@CKM@@a at 2007-7-12 23:10:23 > top of Java-index,Java Essentials,New To Java...
# 3

oops!, final modifier was just an experiment, it shudnt be there, assume that it is not there

anyways, i understood that static methods cannot be overriden

I wonder how it allows me to compile, when in C++ i get an error if i use virtual and static at the same time

Message was edited by:

enterneo

Message was edited by:

enterneo

enterneoa at 2007-7-12 23:10:23 > top of Java-index,Java Essentials,New To Java...
# 4
final or not this will not compile.Edit: Or so I thought but it clearly does. *scratches head*
ChristopherAngela at 2007-7-12 23:10:23 > top of Java-index,Java Essentials,New To Java...
# 5
> final or not this will not compile.> > Edit: Or so I thought but it clearly does.> *scratches head*Why shouldn't it compile if you remove final from the method?
kajbja at 2007-7-12 23:10:24 > top of Java-index,Java Essentials,New To Java...
# 6
I would have expected a warning at least.
ChristopherAngela at 2007-7-12 23:10:24 > top of Java-index,Java Essentials,New To Java...
# 7

> I would have expected a warning at least.

uh, no.

It is quite acceptable (technically) to hide a static method behind another static method.

It's bad form, generally frowned upon, but there's no reason it shouldn't be technically possible (and there may in fact sometimes be a semi-legitimate purpose behind it, though in such cases the method usually shouldn't have been static to begin with).

jwentinga at 2007-7-12 23:10:24 > top of Java-index,Java Essentials,New To Java...
# 8

> and there may in fact sometimes be a semi-legitimate purpose behind it,

> though in such cases the method usually shouldn't have been static to begin with

I thought the reason was for this case:

//today:

class A {}

class B extends A {

public static void f() {}

}

//tomorrow:

class A {

public static void f() {}

}

class B extends A {

public static void f() {}

}

It was considered desirable that adding f to A doesn't break B.

Imagine A is a class in some library and B is a client-written class.

BigDaddyLoveHandlesa at 2007-7-12 23:10:24 > top of Java-index,Java Essentials,New To Java...
# 9
> I wonder how it allows me to compile, when in C++ i> get an error Because C++ != Java.
jverda at 2007-7-12 23:10:24 > top of Java-index,Java Essentials,New To Java...
# 10
>I wonder how it allows me to compile, when in C++ i get an error if i use virtual and static at the same timeBecause in Java you can't use virtual and static at the same time, because virtual isn't part of the language, so you didn't write it.
ejpa at 2007-7-12 23:10:24 > top of Java-index,Java Essentials,New To Java...
# 11

> Because in Java you can't use virtual and

> static at the same time, because virtual isn't part

> of the language,

Well, not explicitly. Every non-private, non-final, non-static method is virtual--or at least polymorphs in the way virtual specifies in C++. But then, that may be a bit of a stretch to begin with, since, as I hope the OP unerstands now, Java != C++.

jverda at 2007-7-12 23:10:24 > top of Java-index,Java Essentials,New To Java...