Ashis Malhotra
Ashis Malhotra

Reputation: 23

Cannot add value to the y variable

Why do I get an error in y=20

class A{
    public static void main(String args[]){
        int x=100;
        final int y=200;
        System.out.println(x+" "+y);

        x=10;
        y=20;
    }   
}

Can anyone explain me this?

Upvotes: 0

Views: 71

Answers (3)

Zak
Zak

Reputation: 26

when you put final before a variable or a method or a class it means it can be changed again in an other place . EX final y = 20 it will be always 20 you can't assign a new value for it . you can read about it her : http://javarevisited.blogspot.com.tr/2011/12/final-variable-method-class-java.html

Upvotes: 1

Levin Mendric
Levin Mendric

Reputation: 24

Because y is a final variable. You can only onece add a value to the final variable. Just one time. And you've did that when create the variable.

final int y = 200;

for more information visit here http://www.javatpoint.com/final-keyword

Upvotes: 1

AlBlue
AlBlue

Reputation: 24060

The y variable is marked as final which means it cannot be changed once set.

Upvotes: 5

Related Questions