• 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 all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Liutauras Vilda
  • Bear Bibeault
  • Tim Cooke
  • Junilu Lacar
Sheriffs:
  • Paul Clapham
  • Devaka Cooray
  • Knute Snortum
Saloon Keepers:
  • Ron McLeod
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Frits Walraven
Bartenders:
  • Carey Brown
  • salvin francis
  • Claude Moore

Why inside a class, outside a method a variable when subjected to an action generates an error?  RSS feed

 
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Why inside a class, outside a method a variable when subjected to an action generates an error? For example



The above, when compiled,  generates the below error. This is the case with Strings and other data types. Some type of error is displayed when compiled



 
Marshal
Posts: 64089
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
That is because you are not allowed statements outside methods etc.
 
Varuna Seneviratna
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If n on the right side in n = n/5 is changed to another variable name no error is given and for another case

public class Emp{
     LocalDate birthDay = LocalDate.of(2019, 3, 10) // This also doesn't give an error

}
 
Varuna Seneviratna
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Varuna Seneviratna wrote:If n on the right side in n = n/5 is changed to another variable name no error is given and for another case

public class Emp{
     LocalDate birthDay = LocalDate.of(2019, 3, 10) // This also doesn't give an error

}



Not n on the right side, it is the n on the left side(LHS)
 
Sheriff
Posts: 24366
55
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Varuna Seneviratna wrote:If n on the right side in n = n/5 is changed to another variable name no error is given




Yes, that's right. It's okay to put declarations in a class outside of any method. It's just not okay to put statements there.

(That ought to prompt you to review the difference between declarations and statements.)
 
Varuna Seneviratna
Ranch Hand
Posts: 197
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Isn't still a statement, even though was changed to
 
Campbell Ritchie
Marshal
Posts: 64089
215
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Varuna Seneviratna wrote:Isn't . . . still a statement . . .

No. It is a field declaration.
 
Marshal
Posts: 13441
222
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When you specify a type before the variable name, that is a declaration. Java allows you to initialize the variable being declared in the same line, which then makes it look like an assignment statement. Your confusion seems to be around the difference between a declaration/initialization and a plain old assignment statement.

The difference is simple: a declaration/initialization has a type specified on the left side whereas an assignment statement does not. Assignment statements must be inside a a code block, like a constructor, a method, or an initialization block.
 
Marshal
Posts: 6684
464
BSD Linux Mac OS X VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

Paul Clapham wrote:It's okay to put declarations in a class outside of any method. It's just not okay to put statements there.


With all respect Paul, I think it might read a bit confusing, not sure how that should sound more precisely though, but...

I think we all learned, that in Java, statements end with ";". So I guess seeing declaration ending with ";" makes to think that it is indeed statement and not something else - and of course that is true, regardless what kind of statement that is. However, I just looked up on google and found several occasions where the distinction is mentioned as "Declaration statements" and "Expression statements".

So maybe more precise answer would be:

Java, outside class allows to write "Declaration statements", while prohibits "Expression statements".
 
Campbell Ritchie
Marshal
Posts: 64089
215
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
When I looked up statements in the JLS this morning, I didn't see anything about declaration statements. I think people are going around creating new terms like. “declaration statement,” and. “expression statement,” and confusing people. Of course a ewb search can't tell you whether you are finding anything good; I bet I know where the next search for “declaration statement” will lead
Actually, I think “expression statement” is an official Java® term.
The fact that the JLS says things like,

If a declarator in a field declaration has a variable initializer, then the declarator has the semantics of an assignment (§15.26) to the declared variable.

I know what it means, but beginners wouldn't know.
 
I’m tired of walking, and will rest for a minute and grow some wheels. This is the promise of this tiny ad:
Create Edit Print & Convert PDF Using Free API with Java
https://coderanch.com/wiki/703735/Create-Convert-PDF-Free-Spire
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!