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


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Java » Java in General
Bookmark "declaring methods" Watch "declaring methods" New topic
Author

declaring methods

anish jain
Ranch Hand

Joined: Feb 03, 2010
Posts: 129
why cannot we declare a method inside a method in java?
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

If you could create it, how would you call this method?

JavaRanch FAQ HowToAskQuestionsOnJavaRanch
Martin Vanyavchich
Ranch Hand

Joined: Sep 16, 2008
Posts: 241
What would be practical use of such method?

You can make method inner classes and have methods defined there ...


SCJP 6, OCMJD 6, OCPJWSD 6
I no good English.
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Paul Sturrock wrote:If you could create it, how would you call this method?

(Same way as any other method :)
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

Martin Vanyavchich wrote:What would be practical use of such method?

Scope overriding.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

David Newton wrote:
Paul Sturrock wrote:If you could create it, how would you call this method?

(Same way as any other method :)


How could you call a public method that is a member of another method?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

It would be method-local, just like in a nested JavaScript function.
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

Wouldn't that restrict the language? I'd have to implement the same "inner" method redundantly accross a bunch of classes. OK it works with a weakly typed scripting language but I can't see how it would give any benefit to a strongly typed language?
David Newton
Author
Rancher

Joined: Sep 29, 2008
Posts: 12617

I didn't claim it would be a benefit--just how it would work. I think it'd be a bad idea in Java.
Michael Angstadt
Ranch Hand

Joined: Jun 17, 2009
Posts: 273

You can do that sort of thing in Scala.


SCJP 6 || SCWCD 5
Paul Sturrock
Bartender

Joined: Apr 14, 2004
Posts: 10336

David Newton wrote:I didn't claim it would be a benefit--just how it would work. I think it'd be a bad idea in Java.


Fair enough. I just couldn't think of any purpose of this either.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: declaring methods