• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Array declaration confusion

 
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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};

}
}
 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 1282
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 142
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 ]
 
reply
    Bookmark Topic Watch Topic
  • New Topic