Reputation: 1011
I am very new to Java have just started working on simple example programs.
How can I create class A's instance inside class B's constructor. For example, I want to create an array of objects of class A , in class B's constuctor. The psudo code would look like
class B {
public static A myarray;
B (int number){
myarray = new A [number];
}
Edited:
public class TestClassA {
public static int [] ArrayA = new int [6];
TestClassA () {
for (int i=0; i < 6; i++){
ArrayA[i]=i;
System.out.print("TestClassA ");
}
}
}
public class TestClassB {
public TestClassA [] A;
TestClassB (int num) {
A = new TestClassA[num];
}
}
public class Exec {
public static void main (String[] args) {
TestClassB B;
B = new TestClassB(2);
}
}
When I execute this, I don't see any messages as "TestClassA ". I expect it to create 2 instances of TestClassA array , hence I should see the TestClassA 12 times. Not sure where am I doing wrong.
Upvotes: 1
Views: 2324
Reputation: 51721
Couple of pointers
static
, unless you want to share the array of A
objects with every instance of class B
.[]
while declaring the reference to indicate that it's an Array
.private
as well. Then control access to them through public
or protected
getter/setter methods.Your code should look like
public class B {
private A[] arrayOfAobjects;
B (int number) {
arrayOfAobjects = new A[number];
}
public A[] getArrayOfAobjects() {
return arrayOfAobjects;
}
}
EDIT: (to elaborate on @MikeStrobel's comments below)
When you create an array, it gets initialized with default values as per the type of the Array
. For example, every array element is set to 0
for an int []
, 0.0
for a double []
, null
for all types of Object []
object arrays.
new int[100]; // creates an Array with 100 zeroes
new A[number]; // creates an Array of size "number"
// but filled with nulls (instead of A objects)
So, you need to populate the arrays with correct values yourself. In your case, something like
B (int number) {
arrayOfAobjects = new A[number];
for (int i=0; i < number; i++) {
arrayOfAobjects[i] = new A(); // initialize the A[] array
}
}
EDIT 2 :
public class TestClassB {
public TestClassA [] A;
TestClassB (int num) {
A = new TestClassA[num];
for (int i=0; i < num; i++) {
A[i] = new TestClassA(); // You need to INITIALIZE your Array
}
}
}
Upvotes: 4
Reputation: 12764
If you want to create an object of class A inside the constructor of class B you can simply do it like this:
class B {
public A object;
B (int number){
object= new A();
}
class A{
}
If you want to create an array of A class then do not make the variable as static.
class B {
public A[] myarray;
int number = 5;
B (int number){
myArray = new A[number];
}
class A{
}
EDIT: Syntax of array of objects(You need array of 4 objects).
A[] a = new A[4]; // Create the array of size 4.
A a1 = new A(); //Create an object
............ //Similarly create other three objects
a[0] = a1; //Add the object to the array
............ //Similarly add other three objects
Upvotes: 1
Reputation: 61550
public class B {
private A[] arrayOfAs;
public B (int number) {
this.arrayOfAs = new A[number];
}
public getAs() {
return this.arrayOfAs;
}
}
Not above that arrayofAs
is not static, because you don't want to share your array among all instances of B
. I also made it private because that is considered good practice for instance variables.
You can return this array to a different class using the getAs()
accessor method above.
Upvotes: 0
Reputation: 3131
This is what you should be writing.
class B{
public A[] myArray;
B(int number){
myArray = new A[number];
}
}
Upvotes: 0
Reputation: 8873
class B {
public A[] myarray;
B (int number){
myarray = new A [number];
}
it would be good to practice to use instance variables in private or protected mode, and using getter and setter methods to access it. This code will just create a un-initialized array of A
objects only. If you want to use those, you need to init them seperately like myarra[i]=new A();
where i
being any number 0<=i<number
Upvotes: 0