Reputation: 131
public class TestVariableDeclaration{
int j; // ERROR
j=45; // ERROR
static{
int k;
k=24;
}
{
int l;
l=25;
}
void local(){
int loc;
loc=55;
}
}
Upvotes: 2
Views: 348
Reputation: 1135
This is very basic java stuff.
(edit:typos)
Upvotes: 2
Reputation: 371
Why dont you simply initialize and declare it together like this -> int j=45;
? It works that way for me..
Upvotes: 0
Reputation: 784908
You cannot use a variable before declaring it under normal circumstances. So
j=45;
at the top will fail since j
hasn't been declared yet.
Unless I am not getting your question, this is very much possible with:
class SomeClass {
int j; // declare it
{
j=45; // initialize it
}
}
OR even more concise:
class SomeClass {
int j = 45; // declare and initialize
}
Upvotes: 0