• 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
  • Paul Clapham
  • Tim Cooke
  • Jeanne Boyarsky
  • Liutauras Vilda
Sheriffs:
  • Frank Carver
  • Henry Wong
  • Ron McLeod
Saloon Keepers:
  • Tim Moores
  • Frits Walraven
  • Tim Holloway
  • Stephan van Hulst
  • Carey Brown
Bartenders:
  • Al Hobbs
  • Piet Souris
  • Himai Minh

Compilation error sequence: Static compilation error coming after resolving access modifier error?

 
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi, I'm new to this forum, so do bear my approach of asking

In the below piece of code:



When compiled, the statement would come as the first compilation error and then the statement would come as the compilation error, once I resolve the first one.

Q - Why doesn't the static variable reference compilation error come first ?
 
Marshal
Posts: 76403
364
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch

I presume the public in line 9 confuses the javac tool about where it is in the code. This is what happened when I tried to compile your code on JShell:-

|Error:
|  illegal start of expression
|          public int var2 = 20;
|          ^
|  Error:
|  <identifier> expected
|          System.out.println("var2 = " + var2);
|                            ^
|  Error:
|  illegal start of type
|          System.out.println("var2 = " + var2);

The non‑static access message didn't appear until after I corrected that first error, like what you said. The compiler appears to be making several passes through the code, and some errors are obviously obscuring other errors. This is an implementation detail of the compiler, which we know nothing about, unless you go through the code.

You would appear to have a very old book if you are writing String args[] rather than String[] args.
 
Lokeshwar Tailor
Greenhorn
Posts: 11
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You're right Campbell. I found some reference @ https://stackoverflow.com/a/17966582/7415499

There are a series of steps performed by the compiler and if either of them fail sequentially, it stops right there.

PS: String args[] is something, I used to type earlier, however touch typing subconsciously didn't registered String[] args

Thanks for your response.
 
Campbell Ritchie
Marshal
Posts: 76403
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lokeshwar Tailor wrote:. . . , however touch typing subconsciously . . .

Hahahahahahahahahahaha!

Thanks for your response.

That's a pleasure
 
Rancher
Posts: 144
9
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
In this case it's about public be an access modifier but it doesn't fit there syntax wise: When declaring local members inside a method they're not allowed to have such modifiers. TLDR: It's a syntax error. This is a very early stage in the compiler run to check if the code meets the syntax rules in the first place. As this example doesn't it already fails at this part.
Checking access context (static vs non-static) comes in a later processing step of the compiler.

Keep in mind: For the compiler checks it doesn't matter in wich order the code is written (same is true for any other order) but what type of error it is and in wich stage of the compiler run it is checked.
Maybe try to think about it this way: When you want to start your car after you rebuild the engine after it was taken apart an early step would be to check if each piece is where it belongs and that each seal is tight. This leakage check would come before you even consider checking wich liquid you fill into wich port - as it doesn't matter if the loop has a leak.
 
Sheriff
Posts: 17068
298
Mac Android IntelliJ IDE Eclipse IDE Spring Debian Java Ubuntu Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Kristina Hansen wrote:Maybe try to think about it this way: When you want to start your car after you rebuild the engine after it was taken apart an early step would be to check if each piece is where it belongs and that each seal is tight. This leakage check would come before you even consider checking wich liquid you fill into wich port - as it doesn't matter if the loop has a leak.


Interesting analogy. I would think that checking which liquid was filled into which “port” is a pretty important check though since your engine would never start if you put windshield washer fluid into the gas tank.  

I agree with Campbell, it’s a compiler implementation detail. However, if I were to guess, I’d agree that the compilation process was doing multiple passes and that syntax checking was being done before symbol resolution, which makes sense. Since having a public declaration in a local context is a syntax error, that would be reported before an invalid attempt to access an instance variable in a static context (a semantic rather than syntactic violation).
 
Kristina Hansen
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Off-Topic: You'll be surprised what some cars can use as fuel. There's a german YouTube channel wich ran a car with sand and iron filings in the gearbox without lube oil, used a mixture of WD40, old lube oil, windshield washer flui, antifreeze and plant oil as fuel - and it still ran for about 25km until the engine gave up (mostly cause they finished it by try to use regular gasoline in a diesel engine - wich obvious doesn't work cause of the missing spark plug). Sure one should check what fluid to fill into wich port - but I guess one wouldn't check if the more critical reason is that the fuel line wasn't connected at all - wich again fits the analogy about what a compiler checks before some other stuff.
 
Campbell Ritchie
Marshal
Posts: 76403
364
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Lokeshwar Tailor wrote:. . . . I found some reference @ https://stackoverflow.com/a/17966582/7415499  . . .

I did read that,and it was good.

Kristina Hansen wrote:. . . sand and iron filings in the gearbox . . .

A brand‑new BMW531i, maybe?
 
Kristina Hansen
Rancher
Posts: 144
9
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Campbell Ritchie wrote:

Kristina Hansen wrote:. . . sand and iron filings in the gearbox . . .

A brand‑new BMW531i, maybe?


No, a VW Caddy ready for the crusher ...
 
I carry this gun in case a vending machine doesn't give me my fritos. This gun and this tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic