• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

java question

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.
 
Back
Top