0

I have question about if I could call a constructor in another constructor in abstract class. For example,

public abstract class Sth{
  protected Sth(){}
  protected Sth(int number){}
  protected Sth(String word){
    int number = 0;
    this(number);
  }

It seems that Java does not allow me to do this. I wondered if there is any way we could let this happen? Thanks

-----------------------------------------------Explanation---------------------------------------------------------------------- The goal I want to do is to call the second constructor specifically. I got error with must calling the first constructor in Java. So I wondered if we can just skip first constructor without remove any code here. Sorry for the confusion.

Jerrold Gao
  • 61
  • 1
  • 9
  • 2
    Well yes: a) you're missing a semi-colon; b) in the `Sth(String word)` constructor, there's no `number` variable in scope... that has nothing to do with it being an abstract class. What did you expect it to do? – Jon Skeet Oct 05 '16 at 03:13
  • You *can* call one constructor from another. Make an example that shows what problem you are having, instead of this bogus thing that doesn't even compile, and include the specific error you get. – Nathan Hughes Oct 05 '16 at 03:25
  • I just briefly write it. Yes, it miss semi colon and I ignore to initial number as int, just make code short and still readable. Eh...I probably should add it. But it does not work if you add those things.@JonSkeet – Jerrold Gao Oct 05 '16 at 03:28
  • I tried to avoid to use real example since it is just a minor problem for my homework. I can just copy paste from what I have in Sth(int number). However, it turns out I want the code become more precise. And this is why I got this problem@NathanHughes – Jerrold Gao Oct 05 '16 at 03:32
  • Jerrold, please understand the problem with the initial post was that a) you didn't describe what problem you were having, and b) the code wasn't something that anybody could run to reproduce your problem. Once you posted something that depicted your problem this became answerable. – Nathan Hughes Oct 05 '16 at 03:49
  • @NathanHughes, The link you give is a different answer there. Actually before I ask this, I read that answer. It does not help for this question. In addition, the question does not aim at how to write but to write it more precisely. But thanks to let me know what you think. I shall update some explanation to it. – Jerrold Gao Oct 05 '16 at 04:03
  • if you want something different from what's answered in the duplicate target it's not evident in the question. I don't know what you want. – Nathan Hughes Oct 05 '16 at 04:08
  • @NathanHughes, I update my question. Could you cancel the duplication mark there? – Jerrold Gao Oct 05 '16 at 04:12
  • I do not get why it is unclear. Since I list my goal is to directly call the second constructor there. Anyway, I guess I waste my time here to argue with you. I'd better discover it in some JVM manual.@NathanHughes – Jerrold Gao Oct 05 '16 at 04:25
  • of course you can call any constructor. are you looking for the rationale of how you decide which constructor to chain to another? anyway, this is back to you not providing example code that allows people to reproduce your problem. – Nathan Hughes Oct 05 '16 at 04:28

2 Answers2

0
  1. Line 6 Constructor call must be the first statement in a constructor
  2. Finally, a closing parenthesis is missing in the end of code

You want the following code?

public abstract class Sth {
    protected Sth() {}
    protected Sth(int number) {}
    protected Sth(String word, int number) {
        this(number);
    }
}
chengjf
  • 34
  • 3
0

In fact, the answer is, the abstract class constructor is not usable. So, we cannot instantiate it. Check Java docs

Jerrold Gao
  • 61
  • 1
  • 9