Questions tagged [class-constructors]
109 questions
13
votes
6 answers
Returning in a static initializer
This isn't valid code:
public class MyClass
{
private static boolean yesNo = false;
static
{
if (yesNo)
{
System.out.println("Yes");
return; // The return statement is the problem
}
…

Martijn Courteaux
- 67,591
- 47
- 198
- 287
12
votes
1 answer
Laravel command cannot call $this->info() in child class
I'm just starting out with the basic concepts OO in PHP,
Foo.php
class Foo extends Command {
public function __construct()
{
parent::__construct();
}
public function fire()
{
$bar = new Bar();
…

Helen Che
- 1,951
- 5
- 29
- 41
11
votes
2 answers
Delphi XE: class constructor doesn't get called in a class using generics
Consider the following example (I am using Delphi XE):
program Test;
{$APPTYPE CONSOLE}
type
TTestClass = class
private
class constructor CreateClass();
public
constructor Create();
end;
class constructor…

Schafsmann
- 113
- 5
11
votes
3 answers
What's ```__new__``` by default in Python 3?
I believe I have some sort of understanding of what __new__ is supposed to do (create an instance, of a class, but not initialize it, that is the job of __init__). I'd like to understand, however, what Python 3 implements as a __new__ method by…

Ignacio
- 377
- 3
- 12
10
votes
2 answers
Can I use C++ class members initialized in the initializer list, later in the list?
I am rewriting some code to eliminate global variables and made a class constructor/destructor handle cleanup of some third party library resources, but I am concerned about some code which initializes one member from another member in the class…

Kenny Ostrom
- 5,639
- 2
- 21
- 30
8
votes
2 answers
How to write API documentation for constructors in Java
Do I have to write param and return tags for constructors in Java for API documentation?
This is the code I have:
/**
* Another constructor for class Time1
*/
public Time1 (Time1 other)
{
_hour = other._hour; _minute = other._minute;…

user1454994
- 303
- 3
- 5
- 12
6
votes
6 answers
Visual Studio 2022 not showing syntax errors and Intellisense is not working properly
Working on a project I made a class with properties and when I tried to auto-generate a class constructor I got an error saying something on lines of "auto generation of class constructor failed... and will be disabled" and ever sense then the…

JDD_11
- 63
- 1
- 1
- 3
6
votes
3 answers
Why am I forced to reference types in unused constructors?
Let's say that I have a class (ClassA) containing one method which calls the constructor of another class, like this:
public class ClassA
{
public void CallClassBConstructor()
{
using(ClassB myB = new ClassB()) {...}
}
}
The…

Stig Perez
- 3,595
- 4
- 23
- 36
6
votes
4 answers
function() : this(null) {}
Can someone please explain the following syntactic sugar?
protected MyConstructor() : this(null)
Mainly I am interested in this part: ": this(null)"
I know how protected, constructors and "this" keyword work, but am confused and cannot find any…

Seth Eden
- 1,142
- 2
- 20
- 42
5
votes
2 answers
how to set the values of one constructor with the values of another?
I'm currently in a beginner java course at my university and am still learning the basics of programming. This week we've been learning about constructors, and I'm stuck on the second half of my assignment for this week so any help would be much…

skulltula
- 153
- 1
- 3
- 10
4
votes
5 answers
"this" Cannot Be Used As A Function
In C++ I'm attempting to emulate how Java handles calls to it's constructor. In my Java code, if I have 2 different constructors and want to have one call the other, I simply use the this keyword. Example:
public Constructor1(String s1, String…

Sam Youtsey
- 884
- 2
- 12
- 33
4
votes
3 answers
Class constructor not called when class registration is done in that class constructor
I am writing a simple dependency injection / inversion of control system based on a TDictionary holding abstract class references with their respective implementor classes.
My goals are:
Avoid direct instantiation by type (obviously).
Inclusion of…

Marjan Venema
- 19,136
- 6
- 65
- 79
3
votes
1 answer
Qt inheritance and classes constructors confusion
I am a bit new to Qt C++ and I think there is a small thing I'm missing but can't figure what is it.
I am trying to make a simple Qt C++ application just to get familiar with it, but I face some problem, First, I have the votor class, which is the…

Mohammed B.
- 160
- 1
- 3
- 13
3
votes
1 answer
Java - Is it possible to have a subclass constructor only 1 parameter, that doesn't involve calling super()?
Here is my abstract class:
public abstract class BankAccount{
protected long balance;
public BankAccount(long balance){ \\<--Abstract class constructor
this.balance = balance;
}
... more stuff
}
I have the following subclass (also an…

Physco111
- 217
- 2
- 7
3
votes
4 answers
Why is my C# constructor not working with the method I'm trying to use?
Maybe I've misunderstood how constructors work, but in any case, I'm trying to create an array and populate it in the constructor.
I have the following code --
class ClsDeck
{
private string[] deck = new string[52];
private string[] hand =…

T1772
- 59
- 2