• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
  • paul wheaton
Sheriffs:
  • Ron McLeod
  • Devaka Cooray
  • Henry Wong
Saloon Keepers:
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
  • Tim Moores
  • Mikalai Zaikin
Bartenders:
  • Frits Walraven

Surprised to see a compile error depending upon order of definition/usage of a class in a method!

 
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If I remember correctly, in the 'C' programming language, a program would not compile if the definition was after the declaration.
I thought Java got rid of that.
So I was surprised to see a compile error depending upon order of definition of a class, and its usage in a method!



 
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Yeah, I suppose it feels a little inconsistent.  Java has a well-defined mechanism for telling the compiler where to look for top-level and member classes, based on the package and import statements in the file you're looking at.  But that doesn't apply for local and anonymous classes, where they revert to the old-fashioned define-it-before-you-use-it philosophy.  In practice I've never seen this create a problem.
 
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:So I was surprised to see a compile error depending upon order of definition of a class, and its usage in a method!


I've been writing code this way for many-many moons with no compile error. What version are you using?
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Carey Brown wrote:I've been writing code this way for many-many moons with no compile error. What version are you using?



Java 17. Can you please copy-paste my code and see if it runs?
 
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Anil Philip wrote:. . . please copy-paste my code and see if it runs?

Have. Doesn't.

The local class A is out of scope where you are trying to use it.
 
Carey Brown
Saloon Keeper
Posts: 10929
87
Eclipse IDE Firefox Browser MySQL Database VI Editor Java Windows ChatGPT
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My mistake, I didn't put it inside a method().
 
Anil Philip
Ranch Hand
Posts: 662
3
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Anil Philip wrote:. . . please copy-paste my code and see if it runs?

Have. Doesn't.

The local class A is out of scope where you are trying to use it.



Two-pass compilers were supposed to take care of this issue (at least that's what I remember from Compilers Theory class in college).
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't know whether javac is a two pass compiler. Order of declaration doesn't usually matter, but local variables, including that local class, have a restricted scope.
 
Saloon Keeper
Posts: 15727
368
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
The thing is, when designing a good compiler, you always have to make trade-offs between performance and ease of use.

A second compiler pass through a method body would definitely allow you to use a local class before it was declared. But is it really worth it?

In a typical Java application, most lines of code that need to be compiled exist inside method bodies, and of those lines of code, the least of them relate to class declarations. Why force the compiler to analyze all these lines of code a second time, only to make a rarely used feature marginally easier to use?
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
[Update: I didn't see Stephan's reply until I posted this, but mine is in a similar vein...]

I imagine the Java compiler does more than one pass of a class in order to compile it, since it needs to be able to know what all the members of a class are in order to be able to reference them out of order.  They presumably build that list of members on one pass, and then on a second pass, use that to determine whether a given method access or field access expression is valid, and if so, what to connect it to.  But apparently, they didn't choose to apply this to allow multiple passes within a method.  Perhaps because they didn't think it would be particularly worthwhile to do so.  I'm sure they could have if they really wanted to - but why would it be useful?

I would argue that aside from technical questions of whether and how they could do this, there's a question of whether it's a good idea.  I think the most relevant concern here, to me, is whether it results in more readable code, or not.  Personally I would find such out-of-order declarations harder to read.  Maybe it's just me - readability is often all about what you're used to, after all.  But I appreciate things that limit the amount of other code I need to read in order to understand a given bit of code.  Declaring things before they are used, for local variables and local types, is one of those things.
 
Sheriff
Posts: 28323
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I vaguely remember a discussion here several weeks (months?) ago about how a local variable had to be declared before it could be used later in the method... perhaps because it was used in a lambda expression, but I'm not sure of that. Anyway the issue was (I think) that declaring the variable after it was used made it be out of scope.
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Mike Simmons wrote:. . . know what all the members of a class are in order to be able to reference them out of order. . . .

That is a feature I have found lacking in C, which makes Java® easier to write. But, as everybody is saying, that doesn't apply to local variables.
Is that local class a member of the surrounding class? I am not quite sure.
 
Mike Simmons
Master Rancher
Posts: 5060
81
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:Is that local class a member of the surrounding class? I am not quite sure.


No.  A "member" of a class must be directly enclosed by that class.  It can't be contained within something else which is a member of the class.  A local class is directly contained by a code block, for a method or constructor or initializer.  It can't be directly contained by a class.
 
Campbell Ritchie
Marshal
Posts: 79956
396
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Thank you. Mike
 
I have gone to look for myself. If I should return before I get back, keep me here with this tiny ad:
Gift giving made easy with the permaculture playing cards
https://coderanch.com/t/777758/Gift-giving-easy-permaculture-playing
reply
    Bookmark Topic Watch Topic
  • New Topic