All Questions
Tagged with constructors java
34 questions
-2
votes
1
answer
300
views
Why aren't constructors atomic? [closed]
If thrown exceptions in constructors can lead to memory leaks or partially-constructed objects, then why we don't make them atomic? so neither an object nor it's local variables will get created/...
8
votes
3
answers
1k
views
Avoiding side effects in immutable class constructor
I rewrote a very long method in which some data is queried from a database, based on info about a particular account, which is queried first.
I split out the account info into an immutable inner ...
-1
votes
2
answers
206
views
Java Constructor Syntax and Design [closed]
With regards to object-oriented Java constructor syntax and design, specifically parameterized or specific constructors, it is the accepted process, as describe in books, to build a parameterized ...
2
votes
1
answer
753
views
Is using the copy constructor in an object's construction is bad? [duplicate]
I was reading this page about using the new keyword in the constructor, and I was wondering if it applies to copy constructors for collections.
Suppose I have a Book class and a collection to store a ...
2
votes
4
answers
872
views
What do OOP languages gain from having constructors that always return an object?
In what seems like a deliberate design decision, C++ does not have a null value for objects and references. This makes using objects and references very elegant since we don't have to perform null ...
1
vote
0
answers
514
views
Are constructors with complex initialization logic always bad? [duplicate]
I've recently read this blog post regarding what a constructor should do and I am also reading Eric Evans' book on Domain Driven Design.
Both the blog post and the book state that a constructor ...
3
votes
4
answers
5k
views
Public static method calls private constructor
I'm working in a C# codebase that was largely written by a former developer and this pattern is used extensively...
public class AuditInserter
{
public static void Insert(
DataContext ...
-4
votes
1
answer
145
views
Can a constructor look like this in Java?
I wonder if a constructor in Java can look like this:
public double[] values;
I'm not sure if the constructor can look like this, or should it be
public values (double[] values){
values = ...
4
votes
2
answers
2k
views
How do we freeze an object while constructing an object using JavaBeans pattern?
Going through Effective Java, Joshua Bloch states that the drawback of using JavaBeans pattern is that an object may be in an inconsistent state partway through the construction and with JavaBeans ...
4
votes
2
answers
631
views
Where should I put the common code of the constructors?
I have a situation where in a class I have 2 constructors and they have very similar code. The only difference is the the call to the constructor of the super class. Where should I put this common ...
2
votes
4
answers
20k
views
When to call the constructor and when to call the method in Java?
I am struggling to fully understand the usage of constructors in Java.
What I have learned so far about constructors is the following:
same name as class
abbreviation ctor
overloading
no return type
...
0
votes
1
answer
5k
views
Object constructors with dynamic parameter lists
I had a quick question and I was hoping someone could help me figure this out. I'm new to Java and I'm trying to learn about classes and objects and I see you can call parameters in the constructor of ...
0
votes
1
answer
99
views
Identifying the use case for instantiation only through existing instances
I was in my CS class when my instructor presented me the following classes:
public class UninstantiableTest {
private static Uninstantiable uninstantiable;
public static void ...
6
votes
6
answers
5k
views
Should constructors ever be used only for side-effects?
Summary: Why is it wrong to design a constructor only for its side effects, and then to use the constructor without ever assigning its return value to a variable?
I'm working on a project that ...
6
votes
4
answers
10k
views
Initializing objects in Constructor
I have below constructor, where it creates a workbook in constructor. I read that, ideally, we should not create objects in Constructor, instead, we should have just assignments which are passed.
...