• Post Reply Bookmark Topic Watch Topic
  • New Topic

Declaretion and Initialization on seperate statement in a class  RSS feed

 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello!

I know that I can declare and initialize an member variable inside a class in a single statement but I can't do them on separate sentence. Why?

For example, I am allowed to do the following,

But I am not allowed to do this,


Why java don't allow me to do that?
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello Quazi,

In this case, when a class doesn't contain main method, you have to initialize variable within constructor.

 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Liutauras Vilda wrote:Hello Quazi,

In this case, when a class doesn't contain main method, you have to initialize variable within constructor.


Thank you. Yes, I can initialize variable in main method. But my question was why java doesn't technically allow me to do inside the class.
 
Mark King
Ranch Hand
Posts: 55
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The reason why the assignment will not work on its own is because the compiler doesn't know when to run it.
 
Liutauras Vilda
Sheriff
Posts: 4928
334
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote:
Liutauras Vilda wrote:Hello Quazi,

In this case, when a class doesn't contain main method, you have to initialize variable within constructor.


Thank you. Yes, I can initialize variable in main method. But my question was why java doesn't technically allow me to do inside the class.


I'm not sure how to explain that "why" in deep technical terms (probably well experienced guys could do so), but constructor defines the initial values of that class, not the class itself.
 
Tomas Linhart
Ranch Hand
Posts: 86
2
Java Ubuntu VI Editor
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote:
Thank you. Yes, I can initialize variable in main method. But my question was why java doesn't technically allow me to do inside the class.

You can do it using initialization block:
 
Jesper de Jong
Java Cowboy
Sheriff
Posts: 16060
88
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
At class level you can only have declarations - for example to declare member variables, constructors, methods etc.

You cannot have statements at class level.

A declaration of a member variable can optionally have an initialization expression (what is after the "=").

Why not: Because, as Mark King already mentioned, there is no way to run statements that are directly at class level. Only methods, constructors etc. can be executed. You cannot execute a class.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tomas Linhart wrote:
Quazi Irfan wrote:
Thank you. Yes, I can initialize variable in main method. But my question was why java doesn't technically allow me to do inside the class.

You can do it using initialization block:


That helped. Thanks.

Jesper de Jong wrote:At class level you can only have declarations - for example to declare member variables, constructors, methods etc.

You cannot have statements at class level.


But the following code compiles without any error.


 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The JLS defines a class member declaration to include semicolon on a line by itself, so yes an empty declaration is permissible.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:The JLS defines a class member declaration to include semicolon on a line by itself, so yes an empty declaration is permissible.


That cleared things up. Thanks.

Since I can't seem to edit my old post, one correction -

 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It isn't an empty statement but an empty declaration.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:It isn't an empty statement but an empty declaration.


I was looking into Java EBNF and I found the following grammar,



If the source is correct, ; is empty statement.
 
Tony Docherty
Bartender
Posts: 3271
82
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote:
Campbell Ritchie wrote:It isn't an empty statement but an empty declaration.


I was looking into Java EBNF and I found the following grammar,



If the source is correct, ; is empty statement.

That comes from the Blocks and Commands section of the document and not the Declarations section and at class level you can only have declarations.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Tony Docherty wrote:
That comes from the Blocks and Commands section of the document and not the Declarations section and at class level you can only have declarations.


I was under the impression that everything that ends with semi colon is a statement, but now I see that's not true at all.

Now what I understand is, everything within a <block> is a statement, everything outside of a <block> isn't a statement.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote: . . .
Now what I understand is, everything within a <block> is a statement . . .
I am afraid that is mistaken. You can have declarations of local variables inside methods. You will have to go through the grammar as printed in the Java Language Specification (=JLS) to find what is and what isn't a statement, but the JLS can be very difficult to read.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:You can have declarations of local variables inside methods.


Yes, this is the only exception I've found so far - that the local variable declaration is still a statement.

From the grammar,


Am I getting it right?
 
James Harte
Greenhorn
Posts: 17
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You can either keep the declaration and initialization in one statement:


or have the initialization in an initialization block.

 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote: . . . the local variable declaration is still a statement. . . .
No, the vertical bar means "OR" in BNF. It means a block statement might be a statement or a local variable declaration statement. It is unfortunate that they include the letters s‑t‑a‑t‑e‑m‑e‑n‑t in both terms.
Similarly, in the official Java® Language Specification. Block statement and statement are different things; a statement is a block statement but a block statement might not be a statement. It actually shows LocalVariableDeclarationStatement as being different from a statement. That is the official nomenclature which the compiler uses, so it can distinuish a LocalVariableDeclarationStatement from a statement.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
James Harte wrote:You can either keep the declaration and initialization in one statement:
. . .
Agree, except that it isn't called a statement.
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Quazi Irfan wrote: . . .
If the source is correct . . .
It doesn't give a date for that BNF; I can't find try with resources or -> or assert in it, so it is Java1.3.1 or older.
 
Quazi Irfan
Ranch Hand
Posts: 104
1
Java Netbeans IDE Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:
Quazi Irfan wrote: . . . the local variable declaration is still a statement. . . .
No, the vertical bar means "OR" in BNF. It means a block statement might be a statement or a local variable declaration statement. It is unfortunate that they include the letters s‑t‑a‑t‑e‑m‑e‑n‑t in both terms.


Question : How do you know everything exists in a class is a declaration, even a semi colon is an empty declaration? Is it because of the following grammar?



It says every member of a class is a ClassMemberDeclaration

And in a class, an initializer block or static block contains statements, right?
 
Campbell Ritchie
Marshal
Posts: 56593
172
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No, it says the syntax to declare each member of the class is a class member declaration.
It doesn't say anything about initialisers or static initialisers in that part of the grammar; you will have to look elsewhere.
There is a difference between something containing statements and something being a statement.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!