default-constructor

Why do we need a default no argument constructor in Java?

人走茶凉 提交于 2019-11-27 03:52:22
问题 Why do we need a default no argument constructor in many Java related APIs? Like as a general rule all java bean classes or entity classes (JPA etc) or JAX-WS implementation classes require a explicit no argument constructor. If by default Java provides a no argument constructor then why most of these standards require a explicit constructor? 回答1: Java only provides a default no-argument constructor if no other constructors are defined . Thus, if you have other constructors you must

Creating a Fragment: constructor vs newInstance()

无人久伴 提交于 2019-11-27 00:06:22
问题 I recently grew tired of constantly having to know String keys to pass arguments into Bundles when creating my Fragments . So I decided to make constructors for my Fragments that would take the parameters I wanted to set, and put those variables into the Bundles with the correct String keys, therefore eliminating the need for other Fragments and Activities needing to know those keys. public ImageRotatorFragment() { super(); Log.v(TAG, "ImageRotatorFragment()"); } public ImageRotatorFragment

“No appropriate default constructor available”--Why is the default constructor even called?

可紊 提交于 2019-11-26 22:00:58
问题 I've looked at a few other questions about this, but I don't see why a default constructor should even be called in my case. I could just provide a default constructor, but I want to understand why it is doing this and what it affects. error C2512: 'CubeGeometry' : no appropriate default constructor available I have a class called ProxyPiece with a member variable of CubeGeometry.The constructor is supposed to take in a CubeGeometry and assign it to the member variable. Here is the header:

When is a private constructor not a private constructor?

↘锁芯ラ 提交于 2019-11-26 21:57:46
Let's say I have a type and I want to make its default constructor private. I write the following: class C { C() = default; }; int main() { C c; // error: C::C() is private within this context (g++) // error: calling a private constructor of class 'C' (clang++) // error C2248: 'C::C' cannot access private member declared in class 'C' (MSVC) auto c2 = C(); // error: as above } Great. But then, the constructor turns out to not be as private as I thought it was: class C { C() = default; }; int main() { C c{}; // OK on all compilers auto c2 = C{}; // OK on all compilers } This strikes me as very

C++ Object Instantiation vs Assignment

旧城冷巷雨未停 提交于 2019-11-26 20:05:55
问题 What is the difference between this: TestClass t; And this: TestClass t = TestClass(); I expected that the second might call the constructor twice and then operator=, but instead it calls the constructor exactly once, just like the first. 回答1: TestClass t; calls the default constructor. TestClass t = TestClass(); is a copy initialization . It will call the default constructor for TestClass() and then the copy constructor (theoretically, copying is subject to copy elision ). No assignment

Should we always include a default constructor in the class?

十年热恋 提交于 2019-11-26 18:38:23
I have been asked this question by a colleague that should we always include a default constructor in a class? If so, why? If no, why not? Example public class Foo { Foo() { } Foo(int x, int y) { ... } } I am also interested to get some lights on this from experts. You have to keep in mind that if you don't provide an overloaded constructor, the compiler will generate a default constructor for you. That means, if you just have public class Foo { } The compiler will generate this as: public class Foo { public Foo() { } } However, as soon as you add the other constructor public class Foo {

Why does the default parameterless constructor go away when you create one with parameters

依然范特西╮ 提交于 2019-11-26 17:34:17
In C#, C++ and Java, when you create a constructor taking parameters, the default parameterless one goes away. I have always just accepted this fact, but now I've started wondering why. What is the reason for this behavior? Is it just a "safety measure/guess" saying "If you've created a constructor of your own, you probably don't want this implicit one hanging around"? Or does it have a technical reason that makes it impossible for the compiler to add one once you have created a constructor yourself? There's no reason that the compiler couldn't add the constructor if you've added your own -

Why don't std::vector's elements need a default constructor?

吃可爱长大的小学妹 提交于 2019-11-26 16:06:43
问题 And how can I write my own array class to not need a default constructor for its elements? Right now, when I do the new [] to allocate space, I need a default constructor. std::vector does not. How do they do this magic? 回答1: std::vector doesn't need the default constructor because it never uses it. Every time it needs to construct an element, it does it by using the copy constructor , because every time it has something to copy: either existing vector element or an element you yourself

Array initialization with default constructor

旧街凉风 提交于 2019-11-26 14:20:47
问题 public class Sample { static int count = 0; public int abc; public Sample() { abc = ++Sample.count; } } I want to create an array of above class, and want each element in the array to be initialized by invoking the default constructor, so that each element can have different abc .So I did this: Sample[] samples = new Sample[100]; But this doesn't do what I think it should do. It seems this way the default constructor is not getting called. How to invoke default constructor when creating an

Default constructors and inheritance in Java

旧时模样 提交于 2019-11-26 12:15:30
问题 I have a question about default constructors and inheritance in Java. Generally, if you write a class and do not include any constructor, Java provides automatically for you a default constructor (one without parameters), which initializes all instance variables of the class (if there are any) with some default values (0, null, or false). If you write a constructor, however, with some parameters, and you don\'t write any default constructor, then Java does not provide a default constructor.