Reputation: 11648
As I'm new to java, I would like to know, 1. How many Default constructors are defined when i Make an Empty Class? 2. Does i need to define copy constructor or it is auto defined as in C++?
Upvotes: 2
Views: 600
Reputation: 70899
There is only one default constructor, which is only defined when you declare no constructors for a class. Otherwise, the declared constructors will be the only constructors. If you do not include a constructor, the compiler inserts code equivalent to
public ClassName() {
super();
}
In addition, if you do declare constructors for a class, and you don't explicitly define the constructor call of the super class, the compiler will insert a parameter matching call to the super class.
public ClassName extends SuperClassName {
public ClassName(String item, List stuff) {
// no explicit super class constructor called
...
}
}
gets transformed in the compiler to something like
public ClassName extends SuperClassName {
public ClassName(String item, List stuff) {
// explicit super class constructor call to SuperClassName(item, stuff);
super(item, stuff);
...
}
}
To define a copy constructor, there is no special syntax, you basically define a constructor that takes another instance of your class as an argument.
public ClassName(ClassName other) {
field = other.field;
field2 = other.field2;
field3 = new ArrayList(other.field3);
...
}
There is no such thing as a default copy constructor, actually there is no such thing as a copy constructor, there are just a constructors. If you decide to create one or more that seem to copy an object, so be it.
The reason that Java needs less types of items is somewhat tied to not overloading basic operators. If you don't put more meanings on =
then you don't need as many types of constructors to support different methods of object allocation.
One of the reasons that =
can be used so simply is due to Java only passing references, which are sort of like pointers; but, they are strongly typed, you cannot do any pointer math, nor access offsets. As such, they are only good for assignment and passing by reference, which prevents most pointer issues.
Upvotes: 7
Reputation: 3342
If no constructor is specified an empty constructor automatically define. As soon as you specify one or multiple constructors only these constructors are defined.
There is no copy constructor but each class in Java extends from Object which as a clone() method.
Upvotes: 1
Reputation: 11
Above all answers are perfectly fine.. Just adding one more thing for more clearly.. Default constructor is one which compiler adds itself when you don't define any constructor... as per above ans :
public ClassName() { super(); }
But if you write constructor yourself in the class... like if you write same constructor your self in the class.. than technically this constructor won't be called default constructor...
I hope you got the point..
Upvotes: 1
Reputation: 420951
1. How many Default constructors are defined when i Make an Empty Class?
A class such as
class MyClass {
}
has one default (empty) constructor which behaves like
...
public MyClass() {
}
...
2. Does i need to define copy constructor or it is auto defined as in C++?
There is no notion of a built-in copy-constructor in Java, and there's a fundamental reason for this:
In Java a variable can never contain an object (it can only contain a reference to an object). So when doing foo(obj)
, then the object which obj
refers to does not need to be copied.
To make a copy of an object you typically create a constructor such as
...
public MyClass(MyClass objToCopy) {
this.field1 = objToCopy.field1;
...
this.fieldN = objToCopy.fieldN;
}
...
or you rely on the clone()
mechanism.
Related questions:
Upvotes: 7
Reputation: 15052
If you don't make any constructor, there is only one default constructor that the compiler implicitly adds. It is the non-parameterized constructor.
class SomeClass{
public SomeClass() // all this is implicitly added if you don't specify anything.
{
super();
}
}
From the documentation:
The compiler automatically provides a no-argument, default constructor for any class without constructors. This default constructor will call the no-argument constructor of the superclass. In this situation, the compiler will complain if the superclass doesn't have a no-argument constructor so you must verify that it does. If your class has no explicit superclass, then it has an implicit superclass of Object, which does have a no-argument constructor.
And in Java, there is no such thing as copy-constructor. If you define one explicitly, then it's a different issue, but the compiler won't ever auto-define such a thing in Java.
Upvotes: 1