commandos2389
commandos2389

Reputation: 175

Does creating new object reseting properties of class

I am new to OO programming. Suppose I have class and 2d array inside full of values. If I create object of a class in another class, is that 2d array going to be empty? If yes, what is the way to preserve values, while making new instance of a class?

Upvotes: 2

Views: 67

Answers (3)

OnlySteveH
OnlySteveH

Reputation: 152

You can use the static keyword to make the member variable common across all instances of the class. That way, every instance will hold the same values in the array.

Upvotes: 3

DMC19
DMC19

Reputation: 927

It will be empty. If you define in the class constructor the method which fill the array, always that constructor is thrown, the array will be full of values.

Something like:

public myclass() {

int array[] = new int[20];
...
}

Upvotes: 0

achAmháin
achAmháin

Reputation: 4266

Create a constructor that takes in the array as a parameter.

public static void main(String[] args) {
        int[][] array = new int[5][10];
        otherClass oc = new otherClass(array);
    }

public class otherClass {

    public otherClass(int[][] a){
        //do stuff with full array
    }
}

Upvotes: 0

Related Questions