-1

if we try to do

class A{
int a = 5 ; 
a=6;  // we will get error
}

i saw this question it like mine but not the same Why can't I do assignment outside a method?

but the answers did not convince me as here

we see that declaration are statement as well as assignment so the class braces should be the block of code containing them, also every statement is inside a block of code for sure in class as a top level or methods as a lower so how they say a statement must be only inside a block of code?

i need an answer or a reference please? thanks for reading.

Community
  • 1
  • 1
Sam
  • 239
  • 2
  • 12
  • "so the class braces should be the block of code" - no, a class declaration doesn't count as a block. It's not really clear what you're trying to achieve here. You *can't* write arbitrary statements in a class - only declarations or initializer blocks. You can read the [JLS](http://docs.oracle.com/javase/specs/jls/se8/html/index.html) for all the details, but fundamentally you need to accept those are the rules. – Jon Skeet Feb 17 '17 at 16:04
  • 1
    I'm not sure what you're asking. Why "should" class braces behave the way you believe they should? They don't; that's just Java. – Dave Newton Feb 17 '17 at 16:06
  • so this did not work because 1) the class body is not a block, if this is true so how it accepts a declaration as the link says it needs a block of code as well . 2) the class body only accepts declaration. i think this is the close answer. – Sam Feb 17 '17 at 16:09
  • 1
    @Sam "How" it accepts a declaration? Because that's how Java is designed. – Dave Newton Feb 17 '17 at 16:09
  • Dave i want to know why did not this work . in a simple way ? – Sam Feb 17 '17 at 16:10
  • ya so the class body contain two different sections: variable declarations and methods . as this link says http://journals.ecs.soton.ac.uk/java/tutorial/java/javaOO/classbody.html – Sam Feb 17 '17 at 16:11
  • 1
    @Sam Because that is the syntax of Java. – Dave Newton Feb 17 '17 at 16:12

1 Answers1

0

In the examples you've posted they are not showing you the complete code. They are actually doing assignments inside methods. That's not to say that assignment inside a class can't be done.

class A {
  int a = 5;
}

This creates a class variable accessible by all the methods in this class, but we can't reassign the variable outside a method. This is because class variables are not ran like code ( not sequentially ).

I would read up on how variables work in Java

ShaneNal
  • 41
  • 5
  • int a= 5 ; this is initialization not assignment . as you say reassign will not work in the class body. cause it will not be accepted as a code! – Sam Feb 17 '17 at 16:18
  • int a = 5; is a declaration and initialization. Assignment was a bad work to use, sorry, initialization is better. Technically it is all code, the reason I said class variables are not treated the same is because class variables are initialized into memory once when the class is loaded. It is not ran sequentially every time it is called like a method. – ShaneNal Feb 17 '17 at 16:23
  • thx i started to get it will search on it more now. – Sam Feb 17 '17 at 16:28