|
Re: Why an abstract method cannot be neither final nor static?
|
Posted: Mar 8, 2005 1:32 AM
|
|
Declairing an abstract method final would not make any sense, would it? You MUST override it, so why should you want to prevent this?
The same for static. The method does not even exist, so why would you want it to be static?
The extending class, however can have this method declared as final. I'm not sure why it can't be declared as static.
Declairing a constructor as private has the effect that you can't create instances from outside using this constructor. For most cases it does not make any sense. But sometimes, you want to use something like a "createThis" method.
public class MyClass {
private MyClass () {
}
private MyClass (parameters) {
//do something impoertant
}
public static MyClass create (parameters) {
//do something important (1). This is not possible, if the constructor is public and you call it directly
try {
MyClass m = new MyClass(parameters);
} catch (ErrorThatCanOccur e) {
return null; //also not possible using the constructor directly
}
//do something important (2)
if (error_boolean_val)
return null; //also not possible using the constructor directly.
return m;
}
}
public class TestIt {
public static void main (String[] args) {
MyClass m = MyClass.create(parameters);
}
}
In the example above I used the create message to do some erro handling. This can come in handy if you must pay attention everytime you need to create a class. This way, you just have to check, if the create method returns null.
|
|