Questions tagged [initializer]

Initializers are called to create a new instance of a particular type. In its simplest form, an initializer is like an instance method with no parameters

684 questions
192
votes
23 answers

static constructors in C++? I need to initialize private static objects

I want to have a class with a private static data member (a vector that contains all the characters a-z). In java or C#, I can just make a "static constructor" that will run before I make any instances of the class, and sets up the static data…
Gordon Gustafson
  • 40,133
  • 25
  • 115
  • 157
138
votes
4 answers

Initializer is inaccessable due to 'internal' protection level

I have a protocol LoginStrategy public protocol LoginStrategy { func login(_ viewController: UIViewController) func getUserInfo(withCompletionHandler completionHandler: @escaping (_ userInfo: [String: Any]?) -> ()) func…
Twitter khuong291
  • 11,328
  • 15
  • 80
  • 116
129
votes
5 answers

Why are C# 3.0 object initializer constructor parentheses optional?

It seems that the C# 3.0 object initializer syntax allows one to exclude the open/close pair of parentheses in the constructor when there is a parameterless constructor existing. Example: var x = new XTypeName { PropA = value, PropB = value }; As…
James Dunne
  • 3,607
  • 3
  • 24
  • 29
113
votes
10 answers

Use of Initializers vs Constructors in Java

So I've been brushing up on my Java skills as of late and have found a few bits of functionality that I didn't know about previously. Static and Instance Initializers are two such techniques. My question is when would one use an initializer instead…
113
votes
9 answers

Static class initializer in PHP

I have an helper class with some static functions. All the functions in the class require a ‘heavy’ initialization function to run once (as if it were a constructor). Is there a good practice for achieving this? The only thing I thought of was…
user258626
  • 1,185
  • 2
  • 8
  • 9
103
votes
8 answers

Calling a Java method with no name

I'm looking at the code below and found something a bit strange: public class Sequence { Sequence() { System.out.print("c "); } { System.out.print("y "); } public static void main(String[] args) { new…
David
  • 19,577
  • 28
  • 108
  • 128
99
votes
2 answers

Static initialisation block in Kotlin

What is the equivalent of a static initialisation block in Kotlin? I understand that Kotlin is designed to not have static things. I am looking for something with equivalent semantics - code is run once when the class is first loaded. My specific…
Marcin Koziński
  • 10,835
  • 3
  • 47
  • 61
77
votes
2 answers

Objective-C: init vs initialize

In Objective-C, what is the difference between the init method (i.e. the designated initializer for a class) and the initialize method? What initialization code should be put in each?
jrdioko
  • 32,230
  • 28
  • 81
  • 120
76
votes
14 answers

C++: constructor initializer for arrays

I'm having a brain cramp... how do I initialize an array of objects properly in C++? non-array example: struct Foo { Foo(int x) { /* ... */ } }; struct Bar { Foo foo; Bar() : foo(4) {} }; array example: struct Foo { Foo(int x) { /*…
Jason S
  • 184,598
  • 164
  • 608
  • 970
73
votes
3 answers

Static constructor equivalent in Objective-C?

I'm new to Objective C and I haven't been able to find out if there is the equivalent of a static constructor in the language, that is a static method in a class that will automatically be called before the first instance of such class is…
Franklin Munoz
  • 917
  • 1
  • 8
  • 13
65
votes
7 answers

Multiple constructors: the Pythonic way?

I have a container class that holds data. When the container is created, there are different methods to pass data. Pass a file which contains the data Pass the data directly via arguments Don't pass data; just create an empty container In Java, I…
Johannes
  • 3,300
  • 2
  • 20
  • 35
60
votes
5 answers

In Ruby, what's the relationship between 'new' and 'initialize'? How to return nil while initializing?

What I want is: obj = Foo.new(0) # => nil or false This doesn't work: class Foo def initialize(val) return nil if val == 0 end end I know in C/C++/Java/C#, we cant return a value in a constructor. But I'm wondering whether it is possible…
Chris Xue
  • 2,317
  • 1
  • 25
  • 21
55
votes
2 answers

How to initialize member-struct in initializer list of C++ class?

I have the following class definitions in c++: struct Foo { int x; char array[24]; short* y; }; class Bar { Bar(); int x; Foo foo; }; and would like to initialize the "foo" struct (with all its members) to zero in the initializer of…
Jan Rüegg
  • 9,587
  • 8
  • 63
  • 105
53
votes
4 answers

Is an empty initializer list valid C code?

It is common to use {0} to initialize a struct or an array but consider the case when the first field isn't a scalar type. If the first field of struct Person is another struct or array, then this line will result in an error (error: missing braces…
Marcus Ahlberg
  • 1,329
  • 2
  • 11
  • 24
51
votes
4 answers

How to handle a static final field initializer that throws checked exception

I am facing a use case where I would like to declare a static finalfield with an initializer statement that is declared to throw a checked exception. Typically, it'd look like this: public static final ObjectName OBJECT_NAME = new…
Romain
  • 12,679
  • 3
  • 41
  • 54
1
2 3
45 46