Win a copy of Functional Reactive Programming this week in the Other Languages forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Array declaration confusion

 
Sushanta Chakrabarty
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1282
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 142
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic