6

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.

1
  • Since you are learning Java, the class name should always start with a capital letter. Commented Dec 11, 2011 at 0:58

6 Answers 6

14

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;
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

@BlagovestBalchev well that's the best part! :)
3

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; 
    } 

} 

Comments

1
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);

Comments

1

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.

Comments

0

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);

Comments

0

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);
    

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.