• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why Java does not support nested functions?  RSS feed

 
Soumya Ranjan Padhi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
whenever we try to define one function another then it shows error.Why it is like that?
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because that's the way Java was designed - the specification says it should behave like that. It's difficult to be more specific than that. But think of it this way - Java, being an object-oriented language, is designed so that all methods belong to a class. So they should be specified directly within a class. A function defined within a method would break that model.

Other languages do it differently.
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
turn the question around...why would you ever need to write a nested function?
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Fred: That can be very useful, and other languages, Scala for example, support this. It's useful for example for small utility methods that you need only inside that one method.

Java doesn't have it, because the designers of the Java programming language didn't add such a feature. There isn't much more to it...
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
We can of course go on to ask "Why did the designers make that decision?" but unless we talk to them personally or get hold of their early white papers, we cannot know for sure. All we can do is speculate, and, like most of these sorts of questions, the likely, generic answer is, "because they felt the cost in complexity would have outweighed the benefit in flexibility, which would go against one of Java's prime goals: simplicity."
 
Soumya Ranjan Padhi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This question was asked to one of my friend in an interview. So it is not about why i want but the question is why it is restricted?
 
Tony Docherty
Bartender
Posts: 3268
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
So it is not about why i want but the question is why it is restricted?

Several people have already answered this, to summarize: because the language doesn't support it and it's impossible for us to say why it doesn't support it as we didn't make the design decision.
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16028
87
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Note that in job interviews, people ask those kinds of questions not because they want you to tell "the right answer", but because they want to know how you think and reason.
 
Soumya Ranjan Padhi
Greenhorn
Posts: 10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
that is absolutely right.but you cann't say something like "Java developers has made it like that" in front of the interviewer.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Note that in job interviews, people ask those kinds of questions not because they want you to tell "the right answer", but because they want to know how you think and reason.

And a good technique, after giving your opinion (and for a question like this, it will only be an opinion, unless you actually wrote the language yourself), is to ask the interviewer what they think the answer is. For one thing, it'll give you an idea if they actually know what they're talking about, or whether they're just "fishing".

Winston
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Soumya Ranjan Padhi wrote:that is absolutely right.but you cann't say something like "Java developers has made it like that" in front of the interviewer.

Sure you can. And to be honest, I probably would. Either that, or I'd ask them to rephrase the question.

Winston
 
Matthew Brown
Bartender
Posts: 4568
9
  • Likes 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Winston Gutkowski wrote:Sure you can. And to be honest, I probably would. Either that, or I'd ask them to rephrase the question.

I'd also speculate in the same way that we have in this thread, while making it clear that it's speculation, and it's a design decision rather than something that couldn't possible work another way.
 
Junilu Lacar
Sheriff
Posts: 11165
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Likes 3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It seems to me that interview questions these days are getting more esoteric and irrelevant to the regular work that a developer has to do. Interviewers who ask questions like this seem to be more about trivia rather than finding out what the candidate can do to help them develop better software. Ridiculous.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Technically, you can emulate a nested function by using an anonymous class. Scala supports nested functions because functions are a basic data type in Scala. THe Java equivalent is anonymous inner class, which makes it difficult but it is possible. I have no idea why you would do something like that when you can just add a private method to the class

 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jayesh A Lalwani wrote:Technically, you can emulate a nested function by using an anonymous class. Scala supports nested functions because functions are a basic data type in Scala. THe Java equivalent is anonymous inner class, which makes it difficult but it is possible. I have no idea why you would do something like that when you can just add a private method to the class

Well, in Scala you'd do it to reduce scope. If you want to create a helper method that is only needed by one other method, then defining it inside that other method is a good way to keep it clear that no other code needs to worry about it. Basically, it's the same reason we make private methods in classes, just on a smaller scale. In Java, those same reasons might apply, but since the Java anonymous class version is so much uglier, and you'd also need an interface or base class to name the method you're calling, in order to be able to call it... it's just not worth the trouble. It's something that's nice to do if the language makes it easy, and not worth the trouble if the language makes it hard.
 
Jayesh A Lalwani
Rancher
Posts: 2762
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote: It's something that's nice to do if the language makes it easy, and not worth the trouble if the language makes it hard.


I'll agree with that!!
 
fred rosenberger
lowercase baba
Bartender
Posts: 12542
48
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Jesper de Jong wrote:Fred: That can be very useful, and other languages, Scala for example, support this. It's useful for example for small utility methods that you need only inside that one method.

I still don't see what it gains you. wouldn't it be just as easy to make a private method in the class (Does Scala have classes? I don't know anything about it)? Sure, the scope may be slightly broader than what you need, but I don't think that is detrimental...
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, do you declare all your local variables at the beginning of the method, as was traditional for years in many older programming languages? Or do you wait to declare them when they're actually used? I prefer the latter. I'm sure that if I were using a language that made it a pain in the @$$ to declare them later, I could declare them up front like people did for years. But since I use languages where it's trivially easy to put the declaration where it makes the most sense to me, I do so. I see local functions in Scala the same way.
 
Angus Comber
Ranch Hand
Posts: 90
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:Well, do you declare all your local variables at the beginning of the method, as was traditional for years in many older programming languages? Or do you wait to declare them when they're actually used? I prefer the latter. I'm sure that if I were using a language that made it a pain in the @$$ to declare them later, I could declare them up front like people did for years. But since I use languages where it's trivially easy to put the declaration where it makes the most sense to me, I do so. I see local functions in Scala the same way.


Exactly. I hate it when people get all defensive and say stuff like who needs a feature like that. They just haven't bothered to think it through.

Java just doesn't support this feature yet. End of story. Lots of other languages don't support nested functions either.
 
Mike Simmons
Ranch Hand
Posts: 3090
14
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't think Fred was defensive; he just doesn't see the benefit, and was asking sincerely. And I think a lot of our notions of readability come from what you're used to - even if you "think it through", as you say, the notion of local functions seems strange at first until you have some time to get used to it.
 
Winston Gutkowski
Bartender
Posts: 10573
65
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Mike Simmons wrote:And I think a lot of our notions of readability come from what you're used to - even if you "think it through", as you say, the notion of local functions seems strange at first until you have some time to get used to it.

Very true. I actually rather like it, because of your point about limiting scope; it just seems self-documenting to me. I wonder if there aren't practical considerations as well, such as code optimization (inlining?), that can be used with a local function.

Winston
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!