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
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
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
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.
If there is no constructor in a class, compiler inserts a default constructor into your code. This constructor is known as default constructor.
class Employee
{
public static void main(String args[])
{
// invoking default constructor
Employee emp = new Employee();
}
}
// Employee.class
class Employee
{
Employee()
{
}
public static void main(String args[])
{
Employee emp = new Employee();
}
}