Default Constructors


#1

How does one create a default constructor that sets the values for certain variables? I am new to this and am learning quickly but need help on this topic.

I am referring to the Java Programming Language. Edited.

Please & Thank You


#2

What language are you referring to in your question? There is no such thing as a default constructor and not all languages have object constructors (Python being one of them).

In JavaScript (ECMAScript, or ES) there are a number of patterns we can use to write custom object constructors, but arrow functions are not among these since they have no this object.

function Foobar(foo, bar) {
    this.foo = foo;
    this.bar = bar;
}

We create instance objects from this constructor by calling it will new

foo = new Foobar('foo', 'bar');
console.log(foo.foo, foo.bar);    // foo bar

#3

Thank you so much for replying quickly. My mistake, I am referring to the Java Programming language. I hope they exist, otherwise whoever wrote this College Textbook saying they do is in trouble. Either way i was wondering if you could help me out. I am told that I need to set a default constructor to set the value of certain variables, lets just say x for now.


#4

https://beginnersbook.com/2013/03/constructors-in-java/