aspose file tools*
The moose likes Beginning Java and the fly likes Leaving blank methods? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Leaving blank methods?" Watch "Leaving blank methods?" New topic
Author

Leaving blank methods?

Ravaa Bal
Ranch Hand

Joined: Apr 15, 2009
Posts: 31
Is leaving methods blank considered bad practice? For example in a preset Java interface, there may be a method 'close()' which you won't really need to use for the rest of the program, would it be okay to code it as:

public void close()
{
}

Thanks.
Maneesh Godbole
Saloon Keeper

Joined: Jul 26, 2007
Posts: 10451
    
    8

How do you think the Adapter classes are implemented?


[How to ask questions] [Donate a pint, save a life!] [Onff-turn it on!]
Abhijit Rai
Ranch Hand

Joined: Aug 07, 2008
Posts: 41
Although it is fine to leave a method blank ,it may be useful if you declare the method inside an interface and implement that interface in your class as blank method.
This way it would be clear that the method was meant to be implemented as a blank method rather than it being left blank by mistake .


SCJP5 ,SCWCD5
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
You might want to put a comment in the blank method to say that it is meant to be blank.
In somecases it might be useful to have the method throw an unimplemented exception if it is called accidentally.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19722
    
  20

Gavin Tranter wrote:You might want to put a comment in the blank method to say that it is meant to be blank.
In somecases it might be useful to have the method throw an unimplemented exception if it is called accidentally.


And the "unimplemented" exception is called java.lang.UnsupportedOperationException


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Ravaa Bal
Ranch Hand

Joined: Apr 15, 2009
Posts: 31
Wow awesome, thank you all for your input
Gavin Tranter
Ranch Hand

Joined: Jan 01, 2007
Posts: 333
Thanks Rob
 
wood burning stoves
 
subject: Leaving blank methods?