-4

I am preparing for OCJP 6.0 exam and I encountered some interesting concepts in Java

  1. Why do we define a class inside an interface? what is the use of it? As we cannot instantiate the class it seems like it does not have any use. Similarly I found that interface can be declared inside class which I think can be used as abstract class. What is the use of it exactly? Possible duplicate : inner class within Interface

  2. what is the concept behind int[] o = new int[][]{{1}}[0]; I am having 1D array and RHS has 3 dimensions still the code compiles??

why so???

Community
  • 1
  • 1
user1079065
  • 2,085
  • 9
  • 30
  • 53

1 Answers1

2

Number 2: int[][]{{1}} initializes a 2D int array, with one row that has one col that has a value of 1. The [0] indexes into that 2D array (at row index 0) and returns the int[] that was initialized which o is assigned to.

It's the same concept as initializing an instance of a class and calling a method on it in the same line, ex:

String name = (new SomeClass()).getName();

Note: You're not forced to save a reference to things when initialized - that's why it looks strange because the reference to the 2D int array is lost.


Number 1: Define Class In Interface - Java. It appears there is plenty of info out there for this question already.

Community
  • 1
  • 1
Andrew_CS
  • 2,542
  • 1
  • 18
  • 38