开发者

Method invocation syntax in Java

I am Java newbie. I have 2 questions.

Question 1: When objDummy is attempted to be created via a constructor call,

Why is there no semi-colon?

How can there be an additional call to init()?

Question 2: Why is there a semi-colon at the comment line "constructor ends"?

Is it a part of Java syntax?

public abstract class A {
  private static final A createD开发者_如何学Cummy() {
   // what is the meaning of the following syntax? Qn. 1
   if (objDummy == null) {
     objDummy = new A (null, null, null){
       final void init(String a, String b, Object[][] cArray) {
       }

       public final boolean isSend() {
         return false;
       }
     // Question 2: format
     }; //constructor ends
   }//if ends
   return objDummy;
  } // function createDummy ends
}//class ends


Question 1: When objDummy is attempted to be created via a constructor call, Why is there no semi-colon?

There is, it's the one you're asking about in Question 2.

What you have there is an anonymous class (the bit starting with new A(null, null, null) { through to the }; you refer to Question 2). That's creating a class on-the-fly, calling its constructor, and returning the resulting instance, which is assigned to objDummy.

How can there be an additional call to init()?

init isn't being called there, it's being defined in the anonymous class. The syntax can be a bit confusing at first, but once you realize that it's defining a class, it gets clearer.

That could could be roughly re-written like this:

public abstract class A {

    private static final A createDummy() {
        if (objDummy == null) {
            objDummy = new ASubclass(null, null, null);
        }
        return objDummy;
    }

    private static class ASubclass extends A {
        final void init(String a, String b, Object[][] cArray) {
        }

        public final boolean isSend() {
            return false;
        }
    }
}

Again, that's not exactly the same (and it won't compile, but neither with the original code; there are missing constructors, missing declaration of objDummy), but hopefully it clarifies a bit. See the link for details. Here's a version that guesses at constructors and such that will compile:

public abstract class A {
    private static A objDummy;

    A(String a, String b, Object[][] cArray) {
    }

    private static final A createDummy() {
        if (objDummy == null) {
            objDummy = new ASubclass(null, null, null);
        }
        return objDummy;
    }

    private static class ASubclass extends A {
        ASubclass(String a, String b, Object[][] cArray) {
            super(a, b, cArray);
        }

        final void init(String a, String b, Object[][] cArray) {
        }

        public final boolean isSend() {
            return false;
        }
    }
}


What you see is an anonymous inner class declaration.

The init() method isn't invoked, it is overridden.

It's like writing:

class Foo extends A {

  /* some constructor declaration goes here */

  final void init(String a, String b, Object[][] cArray) {}

  public final boolean isSend() { return false; }
}

And then

objDummy = new Foo( null, null, null );

It's just compressed into a single expression in which you both define a subclass of A and instantiate it. As for why the syntax is like it is, I think the answer is simply that it was designed this way. It also makes sense though, when you think about what the expression means.


You code defines an anonymous inner class that extends A. This is the same as:

public abstract class A {

private static final A createDummy() {
 // what is the meaning of the following syntax? Qn. 1
 if (objDummy == null) {
  objDummy = new B (null, null, null); //constructor ends
 }//if ends
 return objDummy;
} // function createDummy ends
}//class ends

public class B extends A{
  final void init(String a, String b, Object[][] cArray) {
   }

   public final boolean isSend() {
     return false;
   }
}

But with the only exception that your anonymous inner class doesn't have a class name, and it also has an implicit reference to its containing class. You can use this reference by calling "A.this".

If you look at the code I'll posted, it will become more clear why you need a colon: You basically declare your anonymous inner class in between the last bracket of the constructor of B and the semicolon that follows it.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜