Win a copy of Pipeline as Code this week in the Cloud/Virtualization forum!
  • 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
  • Ron McLeod
  • Paul Clapham
  • Jeanne Boyarsky
  • Bear Bibeault
Sheriffs:
  • Rob Spoor
  • Henry Wong
  • Liutauras Vilda
Saloon Keepers:
  • Tim Moores
  • Carey Brown
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
Bartenders:
  • Frits Walraven
  • Himai Minh
  • Jj Roberts

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

 
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72435
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
That is because you are not allowed statements outside methods etc.
 
Varuna Seneviratna
Ranch Hand
Posts: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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)
 
Marshal
Posts: 26473
81
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

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: 203
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Isn't still a statement, even though was changed to
 
Campbell Ritchie
Marshal
Posts: 72435
315
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

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

No. It is a field declaration.
 
Sheriff
Posts: 16103
268
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
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.
 
Sheriff
Posts: 7950
549
Mac OS X VI Editor BSD Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 72435
315
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
A sonic boom would certainly ruin a giant souffle. But this tiny ad would protect it:
SKIP - a book about connecting industrious people with elderly land owners
https://coderanch.com/t/skip-book
reply
    Bookmark Topic Watch Topic
  • New Topic