constructor

Calendar Constructor Java toString

此生再无相见时 提交于 2019-12-20 02:41:42
问题 What I'm trying to do is pass a date into the Calendar so that it will format the date ready for use with another constructor. So that i can make use of it later using the functions that calendar has provides. public class Top { public static void main(String[] args) { Something st = new Something(getCalendar(20,10,2012)); System.out.println(st.toString()); } public static Calendar getCalendar(int day, int month, int year){ Calendar cal = Calendar.getInstance(); cal.set(Calendar.YEAR, year);

Initialization in definition vs. initialization in constructor [duplicate]

耗尽温柔 提交于 2019-12-20 02:39:13
问题 This question already has answers here : What are the best practices for determining the tasks of Constructor, Initialization and Reset methods (5 answers) Closed 5 years ago . In Java, but in other OO languages as well, is there a difference between initializing an attribute in its definition, as in class Example { public Sample sample_attribute = new Sample(); } and using a constructor to initialize it? class Example { public Sample sample_attribute; public Example() { sample_attribute =

Question about constructors in Java

帅比萌擦擦* 提交于 2019-12-20 02:39:07
问题 I have few questions regarding Java constructors Can a constructor be private? If yes then in which condition? Is a constructor a method or not? If a constructor does not return anything then why we are getting a new Object every time we call it? What's the default access modifier of a constructor if we do not specify. Edit The answers for 1 & 3 are very clear. I'm still not sure about 2 & 4 since I'm getting different answers for them. 回答1: Yes, in any case. However, if all constructors for

Creating a final Java class array of enum constants with values( )

假如想象 提交于 2019-12-20 02:38:25
问题 Inside a Java enumerated class, I'd like to create a final static array containing the values() of the class. When I do this along the following lines, the resulting array is null . public enum Name { E1( stuff ), E2( stuff ); private static final Name[] values = Name.values(); private Name( stuff ) { more stuff; } } I've also tried doing this by calling an explicit class setter method, but this gave an java.lang.ExceptionInInitializerError exception. I understand the problem is caused by

About constructors/destructors and new/delete operators in C++ for custom objects

好久不见. 提交于 2019-12-20 02:35:14
问题 Suppose I have a Linked List I created myself. It has its own destructor, which frees the memory. This Linked List does not overload new or delete. Now, I'm trying to create an array of said linked lists (open hashing, if I understand correctly). Then I allocate the necessary memory inside the constructor of this open hashing class. The new operator being called inside the constructor is enough to correctly allocate the memory for the array, right? I'm not sure because I haven't overloaded

java: new File(“”, “name”) != new File(“name”) ? (file constructor with empty string)

旧街凉风 提交于 2019-12-20 02:17:12
问题 Noticed this today. Given that a file named "existing" exists in the PWD of a java process (windows). new File("existing").exists() => true new File("", "existing").exists() => false new File(".", "existing").exists() => true I would have anticipated, from the javadoc that the system dependent default directory would be "." and these all be true, so this unexpected. Thoughts? Thanks! -roger- 回答1: The two argument constructor expects a parent directory name, so your second line looks for a

no matching function to call to “standard constructor”

六月ゝ 毕业季﹏ 提交于 2019-12-20 02:11:54
问题 I get a weird response from my c++ compiler. I searched the internet but came up with nothing usefull or helpfull... Compiler Response: floating.hpp|line 29|warning: `class HexFloatingPoint' has virtual functions but non-virtual destructor In constructor `HexFloatingPoint::HexFloatingPoint(int, int)': floating.cpp|line 5|error: no matching function for call to `FloatingPoint::FloatingPoint()' floating.hpp|line 16|note: candidates are: FloatingPoint::FloatingPoint(const FloatingPoint&)

Getting NameError when calling function in constructor

谁都会走 提交于 2019-12-20 01:39:57
问题 I ran the code below, by calling the function in the constructor First -- >>> class PrintName: ... def __init__(self, value): ... self._value = value ... printName(self._value) ... def printName(self, value): ... for c in value: ... print c ... >>> o = PrintName('Chaitanya') C h a i t a n y a Once again I run this and I get this >>> class PrintName: ... def __init__(self, value): ... self._value = value ... printName(self._value) ... def printName(self, value): ... for c in value: ... print c

When do superclasses not have a default constructor?

穿精又带淫゛_ 提交于 2019-12-20 01:16:20
问题 According to the Java tutorial on constructors: You don't have to provide any constructors for your class, but you must be careful when doing this. 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

At what condition is the default constructor generated?

情到浓时终转凉″ 提交于 2019-12-19 21:48:48
问题 I have the following class: class Tileset { //base class public: static std::vector<Tileset*> list; virtual ~Tileset() = 0; protected: std::vector<Tile> tiles_list; sf::Texture sheet; private: //non copiable Tileset(const Tileset&); Tileset& operator=(const Tileset&); }; where sf::Texture has a default constructor From my understanding a default constructor should be generated since every member can be default-constructed too. Yet I have a compiler error when I try to construct a derived