Win a copy of Kotlin in Action this week in the Kotlin forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

Why re-initializing the static variable in separate line is invalid?  RSS feed

 
haraprasad mohapatra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please help me understand this why this is not compiled??

Why re-initializing the static variable in separate line is invalid??

public class Test {

static int a;
a =10; //why its invalid

public static void main(String[] arg){
System.out.print(a);
}
}


compilation error: identifier expected why initializing a=10; is not valid whereas static int a=10; is working fine


 
Keith Lynn
Ranch Hand
Posts: 2409
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
It is not because a is static. It is because you can't place code like a=10 outside of a method or other block inside the class definition.
 
Bear Bibeault
Author and ninkuma
Marshal
Posts: 66198
151
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Because you cannot put an executable statement outside a method (or static block).

Please be sure to UseCodeTags in future posts.
 
haraprasad mohapatra
Greenhorn
Posts: 9
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
But i searched for the answer and dint get any where else .

As i am a beginner where can i get this rules like class structure and static rules etc

please guide me
 
Liutauras Vilda
Marshal
Posts: 4636
316
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

A good place to start is Oracle Java Tutorials. Read topic by topic, try out examples given, don't just read them.
Read a lot Beginning Java forum on this website, so you'll see most common problems to solve, approaches.

Research problems a lot on your own, if you feel can't understand it, provide some well presented example with questions, so we could try to clear this together.

Theory, practice, theory, practice. Practice not necessarily means hands on actual coding. That could be diagrams drawing, pseudo code writing on a piece of paper.
Most of exercises can be solved with pencil and piece of paper. In fact, this is where you should start when solving problems.
 
Junilu Lacar
Sheriff
Posts: 11144
160
Android Debian Eclipse IDE IntelliJ IDE Java Linux Mac Spring Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The "Syntax" chapter of the JLS is where you'll find a formal definition of the Java Language syntax. For Java 8, it's here: https://docs.oracle.com/javase/specs/jls/se8/html/jls-19.html

Here's how you would read that:

For the static member declaration, you would start from the section "Productions from §8 (Classes)"
-> ClassDeclaration
-> NormalClassDeclaration
-> ClassBody
-> ClassBodyDeclaration
-> ClassMemberDeclaration (because you declared a static field)
-> FieldDeclaration
-> VariableDeclaratorList
-> VariableDeclarator
-> and so on

(edited) The line of code where you're getting the compiler error is of the form of an Assignment AssignmentStatement, which is under the section "Productions from §15 (Expressions)" "Productions from §14 (Blocks and Statements)"

If you trace the production of an Assignment, you will not be able to match it with any legal productions that will fit the syntax for a ClassMemberDeclaration as defined in the above hierarchy. That's why you get a compiler error, because that line does not follow Java Language syntax rules.

You may ask "How is a beginner supposed to know all this stuff?"

Well, I took my very first programming course in college. This was back in the mid 1980s, in the Philippines. As students in the College of Engineering we were required to learn computer programming and at that time, we were taught Pascal. The book we used was written by Peter Grogono and it included the Pascal language BNF as one of its appendices. Occasionally, our instructor would have us refer to the BNF in the book and walk us through the syntax of a particular language construct. I don't know if I was one of only a few who actually paid attention to our instructor whenever he did that but I found it very useful in understanding how the Pascal compiler worked and why it would give the particular error messages it did when my programs had syntax errors in them.

This early introduction to BNF and my efforts in understanding how to read it helped me tremendously when I took more advanced courses in Computer Science where we had to write our own simple compilers and parsers/lexical analyzers. And I was a Mechanical Engineering student. My point is that I don't think it's a matter of being a beginner or not. You just need enough curiosity and perseverance to wade through those definitions and figure it out. It's kind of like deciphering a puzzle. You start from the top and work your way down, trying to match your code with the patterns defined by the syntax rules. When you can't find a match, you'll know why your code is not allowed.

I admit though that I'm a bit of a nerd and I like that kind of thing so walking through BNF and seeing if my code matched a syntax rule pattern was an interesting exercise for me. ¯\_(ツ)_/¯
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!