aspose file tools*
The moose likes Mock Exam Errata and the fly likes Array declaration confusion Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Soft Skills this week in the Jobs Discussion forum!
JavaRanch » Java Forums » Certification » Mock Exam Errata
Bookmark "Array declaration confusion" Watch "Array declaration confusion" New topic
Author

Array declaration confusion

Sushanta Chakrabarty
Greenhorn

Joined: Dec 05, 2007
Posts: 4
Hi,

In the following code I am getting compilation error on o1 but none on o. Both the declarations are identical. Can you tell me why?

Sushanta



public abstract class AAbst {

Object []o1;
o1= new Object[]{null};
public void main(String[]args){
Object []o;
o= new Object[]{null};

}
}
Muhammed Aslam
Greenhorn

Joined: Feb 03, 2008
Posts: 2
Hi,

I hope the problem is not with the identifier name, its only related to the object initialisation.

please change it to

Object[] o1= new Object[]{null};

because you are not initialising this object inside a method.
Muhammed Aslam
Greenhorn

Joined: Feb 03, 2008
Posts: 2
Hi,

I hope the problem is not with the identifier name, its only related to the object initialisation.

please change it to

Object[] o1= new Object[]{null};

because you are not initialising this object inside a method.
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by Sushanta Chakrabarty:
Hi,

In the following code I am getting compilation error on o1 but none on o. Both the declarations are identical. Can you tell me why?

Sushanta



Try it like this, with the main() as you had it.

harikrishna
Greenhorn

Joined: Jan 29, 2008
Posts: 2
hi,

both may look identical.The difference is that one of them is outside the method body and one is inside.
if you are required to write outside the method then make it in a single statement declaration and initialisation.This cannot be split.

the code below results in a compilation error:

class Test {

int b;
b=10;

}

the code below results in a successfull compilation.

class Test {
int b=10;
public static void main(String args[]){
int b;
b=0;
}
}
Nicholas Jordan
Ranch Hand

Joined: Sep 17, 2006
Posts: 1282
Originally posted by hlakkakula hari:
hi,
... ( snip ) ...
both may look identical.The difference is that one of them is outside the method body and one is inside.


Correctly, where I put it is called a constructor - same meaning as you instruct the original poster.
Ramkumar
Greenhorn

Joined: Apr 18, 2008
Posts: 1
Hi Sushantha



public abstract class AAbst {

Object []o1;
{
o1= new Object[]{null};
}
public void main(String[]args){
Object []o;
o= new Object[]{null};

}
}


putting the o1= new Object[]{null};
in initialization block by adding braces will solve the problem.
Arijit Daripa
Ranch Hand

Joined: Aug 09, 2008
Posts: 142
Dear! you can reinitialize (mind it, instance variables get default values during instantiation) instance variables only in non-static initializer block or in any method including constructor. You never ever ever ever can reinitialize them outside those scopes.



public class Test{
int i; // initialized with b = 0 in this line;
float f; // initialized with f = 0.0f in this line;
Test t; // initialized with t = null in this line;
i = 2; // compilation fails
f = 7.5f // compilation fails
t = new Test() // compilation fails
// no assignent can be possible here
{
i = 2; // Okay, inside non-static initializer block
}
public Test(){
i = 2; // Okay, inside constructor
}
public void setI(int j){
i = j; // Okay, inside method
}
}

[ August 09, 2008: Message edited by: ARIJIT DARIPA ]
[ August 09, 2008: Message edited by: ARIJIT DARIPA ]

SCJP 5
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Array declaration confusion