object-construction

Does the JVM internally instantiate an object for an abstract class?

放肆的年华 提交于 2019-12-05 03:31:58
问题 I have an abstract class and its concrete subclass, when I create an object of subclass it automatically calls the super constructor. Is the JVM internally creating an object of the abstract class? public abstract class MyAbstractClass { public MyAbstractClass() { System.out.println("abstract default constructor"); } } public class ConcreteClass extends MyAbstractClass{ public static void main(String[] args) { new ConcreteClass(); } } then how constructor exists without an object in JVM ??

How do I initialize classes with lots of fields in an elegant way?

删除回忆录丶 提交于 2019-11-29 21:07:54
In my application, I have to instantiate many different types of objects. Each type contains some fields and needs to be added to a containing type. How can I do this in an elegant way? My current initialization step looks something like this: public void testRequest() { //All these below used classes are generated classes from xsd schema file. CheckRequest checkRequest = new CheckRequest(); Offers offers = new Offers(); Offer offer = new Offer(); HotelOnly hotelOnly = new HotelOnly(); Hotel hotel = new Hotel(); Hotels hotels = new Hotels(); Touroperator touroperator = new Touroperator();

JavaScript: using constructor without operator 'new'

醉酒当歌 提交于 2019-11-27 08:35:30
Please help me to understand why the following code works: <script> var re = RegExp('\\ba\\b') ; alert(re.test('a')) ; alert(re.test('ab')) ; </script> In the first line there is no new operator. As far as I know, a contructor in JavaScript is a function that initialize objects created by the operator new and they are not meant to return anything. In general, if something is documented as being a constructor, use new with it. But in this case, RegExp has a defined "factory" behavior for the situation where you've called it as a function instead. See Section 15.10.3 of the ECMAScript

How do I use Powermockito to mock the construction of new objects when testing a method in an anonymous class?

醉酒当歌 提交于 2019-11-27 02:52:34
问题 I woud like to write a JUnit test to verify that the code below uses a BufferedInputStream: public static final FilterFactory BZIP2_FACTORY = new FilterFactory() { public InputStream makeFilter(InputStream in) { // a lot of other code removed for clarity BufferedInputStream buffer = new BufferedInputStream(in); return new CBZip2InputStream(buffer); } }; (FilterFactory is an interface.) My test thus far looks like this: @Test public void testBZIP2_FactoryUsesBufferedInputStream() throws

How can I initialize C++ object member variables in the constructor?

て烟熏妆下的殇ゞ 提交于 2019-11-26 21:41:49
I've got a class that has a couple of objects as member variables. I don't want the constructors for these members to be called when declared, so I'm trying to hang onto a pointer to the object explicitly. I have no idea what I'm doing. o_O On StackOverflow, I seem to be able to find other examples of object member variables, but usually the constructor is called immediately, like this: class MyClass { public: MyClass(int n); private: AnotherClass another(100); // this constructs AnotherClass right away! }; But I want the MyClass constructor to call the AnotherClass constructor. Here's what my

JavaScript: using constructor without operator &#39;new&#39;

大兔子大兔子 提交于 2019-11-26 14:12:15
问题 Please help me to understand why the following code works: <script> var re = RegExp('\\ba\\b') ; alert(re.test('a')) ; alert(re.test('ab')) ; </script> In the first line there is no new operator. As far as I know, a contructor in JavaScript is a function that initialize objects created by the operator new and they are not meant to return anything. 回答1: In general, if something is documented as being a constructor, use new with it. But in this case, RegExp has a defined "factory" behavior for

Why should I prefer to use member initialization list?

岁酱吖の 提交于 2019-11-25 21:41:33
问题 I\'m partial to using member initialization lists with my constructors... but I\'ve long since forgotten the reasons behind this... Do you use member initialization lists in your constructors? If so, why? If not, why not? 回答1: For POD class members, it makes no difference, it's just a matter of style. For class members which are classes, then it avoids an unnecessary call to a default constructor. Consider: class A { public: A() { x = 0; } A(int x_) { x = x_; } int x; }; class B { public: B()