Java Default Member

jjcamachosanchez
jjcamachosanchez used Ask the Experts™
on
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
 
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Top Expert 2016

Commented:
You simply create an instance of the array and assign to it. Statically if necessary

Author

Commented:
I need to create the Matrix2D class because I need more functionality in it than the array
Mick BarryJava Developer
Top Expert 2010

Commented:
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;
OWASP: Threats Fundamentals

Learn the top ten threats that are present in modern web-application development and how to protect your business from them.

Author

Commented:
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;
CPColinSenior Java Architect

Commented:
Short answer: no, there is no concept of a default member in Java.
Mick BarryJava Developer
Top Expert 2010

Commented:
>  I want to access the _matrix member in this way:

you can't, you need to do it as I posted above
Top Expert 2016

Commented:
>>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
Top Expert 2016
Commented:
If you want to access it statically, something like the following would be a way to go
public class Matrix2D {
      public static final int[][] matrix;

      static {
	  matrix = new int[10][10];
	  for(int i = 0;i < 10;i++) {	
	      for(int j = 0;j < 10;j++) {	
	      }
	  }
      }
}

Open in new window

Mick BarryJava Developer
Top Expert 2010

Commented:
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.
Top Expert 2016

Commented:
:)

In fact it won't stop you adding extra functionality

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial