File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes can't the variables be assigned a value inside the class in which its declared? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply locked New topic
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
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
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
    
    1

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?
 
Similar Threads
innerclass
Question Local Variable Scope
simple question
q on testonline.com
varargs - method resolution principle???