D
D
deli812022-01-31 20:53:49
Java
deli81, 2022-01-31 20:53:49

Why can't I initialize an instance variable after it's been declared?

Why can I do this:

public class Main{
int a = 10;
}

But I can't do this:
public class Main{
int a;
a = 10;
}
?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Roo, 2022-01-31
@deli81

A variable can be initialized, but not in the way you do.
Here you can read about the possible options: https://vertex-academy.com/tutorials/en/bloki-inic...

A
Alexander, 2022-02-01
@Arlekcangp

You are trying not to initialize a variable, but a class field. It can be initialized either within a method or when declared. But your initialization lies inside the class definition. And so it is impossible.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question