bbalchev
bbalchev

Reputation: 857

How to pass an array to constructor?

I'm trying to learn Java, but I have a problem with passing an array to constructor. For example:

Application class: byte[][] array = new byte[5][5]; targetClass target = new targetClass(array[5][5]);

Target class:

public class targetClass {
    /* Attributes */
    private byte[][] array = new byte[5][5];

    /* Constructor */
    public targetClass (byte[][] array) {
        this.array[5][5] = array[5][5];
    }

}

I'd greatly appreciate it if you could show me how I can do that.

Upvotes: 5

Views: 83052

Answers (6)

Ashish Mishra
Ashish Mishra

Reputation: 724

To pass an array to a constructor we need to pass in the array variable to the constructor while creating an object.

So how we can store that array in our class for further operations.

  • We need an instance variable to store that in our case it can be private byte[][] array;

  • We don't need to assign memory to it as that would get wasted because later it will be pointing to the original arrays heap location in the memory.

  • We can copy the array using various techniques

      public class targetClass {
      /* Attributes */
      private byte[][] array = new byte[5][5];
    
      /* Constructor */
      public targetClass (byte[][] array) {
          this.array  = array;
          this.array = array.clone();//If you want separate object instance on heap
      }
    }
    byte[][] data = new byte[10][10];
    targetClass t1 = new targetClass(data);
    

Upvotes: 0

Kevin Ji
Kevin Ji

Reputation: 10489

In your application class, the following should work:

byte[][] array = new byte[5][5];
TargetClass target = new TargetClass(array); // Not array[5][5]

In addition, for your target class, the following should work:

public class TargetClass {
    /* Attributes */
    private byte[][] array; // No need to explicitly define array

    /* Constructor */
    public TargetClass (byte[][] array) {
        this.array = array; // Not array[5][5]
    }
}

As mentioned, class names are usually capitalized, so that's what I've done to your class names.

Upvotes: 1

greenLizard
greenLizard

Reputation: 2346

public class targetClass {
    /* Attributes */
    private byte[][] array = null;

    /* Constructor */
    public targetClass (byte[][] array) {
        this.array = array;
    }

}

Then call it like this

byte[][] array = new byte[5][5]; 
targetClass target = new targetClass(array);

Upvotes: 1

merlin2011
merlin2011

Reputation: 75545

I am going to assume that you're trying to assign the private array to the passed in array, rather than trying to pick the 5,5 element out of the passed-in array.

Inside the constructor, the syntax should be:

this.array = array;

In the application, it should be

targetClass target = new targetClass(array);

Upvotes: 0

jatanp
jatanp

Reputation: 4092

You do not need to intialize array in the class at the time of declaration. It can be set to the passed array's reference. For example,

public class targetClass { 
    /* Attributes */ 
    private byte[][] array = null; 

    /* Constructor */ 
    public targetClass (byte[][] array) { 
        this.array = array; 
    } 

} 

Upvotes: 2

MByD
MByD

Reputation: 137272

First, usually class names in Java starts with Upper case, now, to the problem you met, it should be:

public class TargetClass { /* Attributes */ 
    private byte[][] array;

    /* Constructor */
    public TargetClass (byte[][] array) {
        this.array = array;
    }
}

Upvotes: 13

Related Questions