jjcamachosanchez
asked on
Java Default Member
Is there a way to define a default member in Java?
I want to declare a class like:
public class Matrix2D {
public int[][] _matrix;
}
Next declare an instance of this class:
public Matrix2D myMatrix;
Then be able to directly use the 2D internal array "_matrix" like this:
myMatrix[1][1] = 15;
Thanks,
Jhon
I want to declare a class like:
public class Matrix2D {
public int[][] _matrix;
}
Next declare an instance of this class:
public Matrix2D myMatrix;
Then be able to directly use the 2D internal array "_matrix" like this:
myMatrix[1][1] = 15;
Thanks,
Jhon
You simply create an instance of the array and assign to it. Statically if necessary
ASKER
I need to create the Matrix2D class because I need more functionality in it than the array
public class Matrix2D {
public int[][] _matrix = new int[10][10];
}
Next declare an instance of this class:
public Matrix2D myMatrix = new Matrix2D();
you can then access the array using:
myMatrix._matrix[1][1] = 15;
public int[][] _matrix = new int[10][10];
}
Next declare an instance of this class:
public Matrix2D myMatrix = new Matrix2D();
you can then access the array using:
myMatrix._matrix[1][1] = 15;
ASKER
Want I want to do is that the Matrix2D looks like an Array but with more functionality. In order to obtain that, I want to access the _matrix member in this way:
myMatrix[1][1] = 15;
myMatrix[1][1] = 15;
Short answer: no, there is no concept of a default member in Java.
> I want to access the _matrix member in this way:
you can't, you need to do it as I posted above
you can't, you need to do it as I posted above
>>I need to create the Matrix2D class because I need more functionality in it than the array
Yes, naturally i mean that you create an instance of the array *in* the class
Yes, naturally i mean that you create an instance of the array *in* the class
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Can you explain why you accepted that comment?
Doing that will not allow you to add any extra functionality to the array. In fact it will only allow to have a single array so its pretty much useless.
Doing that will not allow you to add any extra functionality to the array. In fact it will only allow to have a single array so its pretty much useless.
:)
In fact it won't stop you adding extra functionality
In fact it won't stop you adding extra functionality