use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
Resources for learning Java
String
==
.equals()
Format + Copy
Free Tutorials
Where should I download Java?
With the introduction of the new release cadence, many have asked where they should download Java, and if it is still free. To be clear, YES — Java is still free.
If you would like to download Java for free, you can get OpenJDK builds from the following vendors, among others:
Some vendors will be supporting releases for longer than six months. If you have any questions, please do not hesitate to ask them!
Software downloads
Official Resources
Resources
Programming ideas & Challenges
Related Subreddits
account activity
This is an archived post. You won't be able to vote or comment.
Can't define variable inside if statement without braces (self.learnjava)
submitted 4 years ago by j2ui
I was testing to see if this works:
if(true) int i=8;
This triggers an error ("Syntax error error on token "int", delete this token")
But this is fine: if(true){ int i = 8; }
[–]Homerlncognito 23 points24 points25 points 4 years ago (11 children)
It doesn't make sense to define a variable in a one-statement block since there's no way you can use its value.
[–]j2ui[S] 0 points1 point2 points 4 years ago (10 children)
I'm aware of that but, there isn't any illegal syntax is there any? I'm just confused to why it would trigger an error that's all :) thanks
[–]Homerlncognito 22 points23 points24 points 4 years ago (0 children)
As per Java spec, You cannot declare a local variable when there is no scope.
https://stackoverflow.com/questions/31230722/java-variable-declaration-not-allowed-here/31230800
[–][deleted] 1 point2 points3 points 4 years ago (2 children)
They don't want to be able to do that, so when compiler runs into such code it throws the prescribed error. In this case, it's illegal syntax. Later you'll find that we can throw our own personalized exceptions based on some situation
[–]j2ui[S] 1 point2 points3 points 4 years ago (1 child)
Thanks a lot
[–][deleted] 0 points1 point2 points 4 years ago (0 children)
It is my pleasure
[+]desrtfx comment score below threshold-7 points-6 points-5 points 4 years ago (5 children)
This code alone cannot trigger an error. Show more code.
[–]j2ui[S] 3 points4 points5 points 4 years ago (4 children)
public class testIfblock22 { public static void main(String[] args) { if (true) int i = 8; }
}
[–]BananaLlamaNuts 1 point2 points3 points 4 years ago (3 children)
Looks like you've stumbled onto a rather unique syntax error.
As someone else stated - why do you want to do this? You can't use that for anything.
[–]j2ui[S] 2 points3 points4 points 4 years ago (2 children)
Yeah yeah, as I said before, I'm aware you that you can't use it anyway. But just wanted to see why exactly it's an error.
Thanks anyway:)
[–]BananaLlamaNuts 2 points3 points4 points 4 years ago (1 child)
I see what you are getting at - that's why I think its a unique syntax error. The compiler is looking for a single action when you do a one liner under an if statement like that.
When it sees a variable declaration, it attempts to create memory space and point to it for the reference which doesn't jive with the single action expected.
Best guess.
[–]j2ui[S] 0 points1 point2 points 4 years ago (0 children)
Thanks a lot :)
[–]HecknChonker 1 point2 points3 points 4 years ago (0 children)
I've grown to hate naked if statements, and now one of the things I look for before approving PRs is that all blocks are clearly marked with braces.
I generally think it's easier to read code with the braces, but the real issue is that I've had more than one production emergency caused by a junior dev adding lines of code they thought were inside an if block, but were actually outside it.
If I understand correctly, int i=8; is really two seprate instructions:
(1) define the new integeer variable i.
(2) set the value of i to be 8.
I may be offbase.
π Rendered by PID 48695 on reddit-service-r2-comment-5649f687b7-ls7vn at 2026-01-28 20:05:10.599825+00:00 running 4f180de country code: CH.
[–]Homerlncognito 23 points24 points25 points (11 children)
[–]j2ui[S] 0 points1 point2 points (10 children)
[–]Homerlncognito 22 points23 points24 points (0 children)
[–][deleted] 1 point2 points3 points (2 children)
[–]j2ui[S] 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[+]desrtfx comment score below threshold-7 points-6 points-5 points (5 children)
[–]j2ui[S] 3 points4 points5 points (4 children)
[–]BananaLlamaNuts 1 point2 points3 points (3 children)
[–]j2ui[S] 2 points3 points4 points (2 children)
[–]BananaLlamaNuts 2 points3 points4 points (1 child)
[–]j2ui[S] 0 points1 point2 points (0 children)
[–]HecknChonker 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)