The Artima Developer Community
Sponsored Link

Java Answers Forum
Why an abstract method cannot be neither final nor static?

1 reply on 1 page. Most recent reply: Mar 8, 2005 1:32 AM by Matthias Neumair

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Jonathan

Posts: 1
Nickname: nikolas
Registered: Mar, 2005

Why an abstract method cannot be neither final nor static? Posted: Mar 7, 2005 10:27 AM
Reply to this message Reply
Advertisement
If a method is abstract it is not possible to make it neither final nor static. Why is that?

What are the consequences of declaring a constructor as private?

Thank you in advance


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Why an abstract method cannot be neither final nor static? Posted: Mar 8, 2005 1:32 AM
Reply to this message Reply
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.

Flat View: This topic has 1 reply on 1 page
Topic: Java + firewalls (iptables) Previous Topic   Next Topic Topic: Swap images not for mouse over

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use