-1

I don't know if can't understand this question because I'm new to programming, or that the question is phrased weird.

I'm familiar with what a constructor is, and understand the first sentence of the question.

I've googled the question but can't find any answers. I just really don't understand what the question is asking from me.

---- edit -------

This would be the first part of the question. Now I don't know what to do with the next.

public class Constructor {
    private String name;
    private int age;

    public Constructor(String name, int age) {
        this.name = name;
        this.age = age;
    }
}

----- edit ----

So, after reading your comments I tried this. It works, but I'm not sure if it answers the question still. (It's a question from a textbook, and not my phrasing)

public class Account {
    private String name;
    private String balance;

    public Account(String name, String balance) {
        this.name = name;
        this.balance = balance;
    }

    public Account() {
        this.name = "Joseph bouff";
        this.balance = "Will greir";
    }

    public void setName(String name) {
        this.name = name;
    }

    public String getName() {
        return name;
    }

    public void setBalance(String balance) {
        this.balance = balance;
    }

    public String getBalance() {
        return balance;
    }
}




public class AccountTest {
    public static void main(String[] args) {
        Account account1 = new Account("Jane Green", "Paul goodman");
        Account account2 = new Account();
        System.out.println(account1.getName() + " " + account1.getBalance());
        System.out.println(account2.getName() + " " + account2.getBalance());
    }
}
BjornarEB
  • 1
  • 5

3 Answers3

0

Java has the concept of a "default constructor". If you haven't defined any constructors, a no-argument constructor will automatically exist. But if you write your own constructor that takes parameters, that default constructor disappears. If you still want a no-arg constructor in that situation, you can just write it yourself. For example:

class Demo {
    private String foo;

    Demo() {
    }

    Demo(String foo) {
        this.foo = foo;
    }

    // Other methods here....
}
Jordan
  • 2,273
  • 9
  • 16
  • "a no-argument constructor will automatically exist" this statement is not correct! You can simply try it, and you'll see a compiler error – pedram bashiri Jul 22 '19 at 20:54
  • 2
    @pedrambashiri `If a class contains no constructor declarations, then a default constructor with no formal parameters and no throws clause is implicitly declared.` [JLS 8.8.9](https://docs.oracle.com/javase/specs/jls/se7/html/jls-8.html#jls-8.8.9) – Michał Krzywański Jul 22 '19 at 20:57
  • @pedrambashiri, I'm afraid that you're mistaken. If you don't define any constructor in Java, you can still write `myClass foo = new MyClass()`. There's no need to specifically declare a no-arg constructor _unless_ you have written a different constructor. Once you write a constructor, the default constructor no longer exists. – Jordan Jul 22 '19 at 20:58
  • @Jordan Thanks for answer. As you just stated, I can't use this solution. Because I HAVE declared a constructor already with two parameters. – BjornarEB Jul 22 '19 at 21:05
  • @Jordan and michalk just like you both said "if a class contains no constructor" which is clearly not the case here. – pedram bashiri Jul 22 '19 at 21:09
  • @pedrambashiri you should read his whole sentence, not just part of it which is out of context. The full sentence is `If you haven't defined any constructors, a no-argument constructor will automatically exist` - which is true. – Michał Krzywański Jul 22 '19 at 21:13
  • Fair enough, I take my comment back :) but still this can't be a correct answer as it is clear in the question that a 2 argument constructor already exists. – pedram bashiri Jul 22 '19 at 21:18
0

If you have access to code where the class is defined, you need to add a constructor that takes no parameter. Otherwise if you try to create an instance of the class without passing parameters you'll get a compiler error

pedram bashiri
  • 1,286
  • 15
  • 21
0

In this case you need to create a new constructor without parameters, In java if there is not any constructor the default constructor will automatically created and if we will declare any constructor with parameter than the default constructor will not created.

Ghulam Murtaza
  • 162
  • 2
  • 6