Bacardi151
Senior member
was wondering how to write a default constructor (that takes no args) and initializes the size of an integer array.
would i write something like this? (although i tried and its not working too well)
class nestedClass {
int[] a;
public nestedClass() {
a = new int[5]; // to initialize the size to 5
}
......
although i did try this on something else and it worked....i wrote the following
____________________________________
class originalClass {
nestedClass[] a;
public originalClass() {
a = new nestedClass[5];
}
this example (which worked) is that the originalClass encapsulates nestedClass member as a nested class
and in the nested class (the top example) i need to keep an integer array, i know i can initialize it by simply writing new int[5] when i declare the array, but the professor wants me to make a default constructor that initializes the array, so i was wondering how i'd do it, i tried doing it the way i just showed but it gives me an error about nullpointer yada yada, so basically it never goes to the constructor
and i was wondering why as it worked in the originalClass, and is it working because nestedClass is a member of originalClass? because otherwise i'm clueless of why one would work over the other.
would i write something like this? (although i tried and its not working too well)
class nestedClass {
int[] a;
public nestedClass() {
a = new int[5]; // to initialize the size to 5
}
......
although i did try this on something else and it worked....i wrote the following
____________________________________
class originalClass {
nestedClass[] a;
public originalClass() {
a = new nestedClass[5];
}
this example (which worked) is that the originalClass encapsulates nestedClass member as a nested class
and in the nested class (the top example) i need to keep an integer array, i know i can initialize it by simply writing new int[5] when i declare the array, but the professor wants me to make a default constructor that initializes the array, so i was wondering how i'd do it, i tried doing it the way i just showed but it gives me an error about nullpointer yada yada, so basically it never goes to the constructor
and i was wondering why as it worked in the originalClass, and is it working because nestedClass is a member of originalClass? because otherwise i'm clueless of why one would work over the other.