| Author |
can't the variables be assigned a value inside the class in which its declared?
|
Rohit Raghunath
Greenhorn
Joined: Apr 01, 2012
Posts: 15
|
posted

0
|
I tried the following code: which gave me compile time error.
class Test
{
int []a= new int[5];
a[0]=4;
int a;
a=9;
}
when I try this it gives no error!!!
class Test
{
int []a= {4,5,6};
int a=9;
}
why cant the variables be initialized by the first code?
|
 |
John Jai
Bartender
Joined: May 31, 2011
Posts: 1776
|
posted

0
|
It's not valid to have the assignment operation outside a block in the class body provided it's not in the same line of variable declaration.
Please UseOneThreadPerQuestion and CodeTags
|
 |
Matthew Brown
Bartender
Joined: Apr 06, 2010
Posts: 3786
|
posted

0
|
|
Duplicate of http://www.coderanch.com/t/581364/java-programmer-SCJP/certification/variables-assigned-value-inside-class. Both have got answers, so I'm going to lock this one, but I'll post a link back because of John's answer.
|
 |
 |
I agree. Here's the link: jrebel
|
|
subject: can't the variables be assigned a value inside the class in which its declared?
|
|
|