Tag Info

New answers tagged

1

But what if it isn't so simple and we really don't know what kind of thing we want to store, only that it is one of a finite set of well-defined things? This is precisely what a tagged union is for. You can emulate them in Java using an abstract base class with a private constructor, and final inner subclasses. The inner subclasses can use the private ...


1

This feels like a "service" class or an implementation of the "unit of work" pattern would be beneficial. I'm leaning towards Unit of Work here. You can define multiple "unit of work" classes in a hierarchy to break up the updates. Plus you can define additional classes for updating the $db_object with values from the $api_object. While this adds ...


1

Along with Robert Harvey's advice, rather than 'setting' an existing API object's properties from an instance of a DB Object directly where you need it to happen, why not just 'create' a brand new API object using a factory that uses your DB object as a template? class ApiObjectFactory { public static function createFromDbObject($dbObject, ...


6

Create alias references for the Location and Address objects to shorten the assignments. $loc = $api_object->Organisation->Location; $main = $db_object->MainAddress; $loc[0]->AddressLine1 = $main->address_line_1; $loc[0]->AddressLine2 = $main->address_line_2;


1

For the sake of simplicity, Java designers decided Strings, despite not being primitives, can be declared and initialized as such. String s = "test"; which is the same as String s = String("test"); Something similar they did with arrays: int[] numbers = {1,2,3}; Those are examples of objects being initialized without explicitly calling their ...


1

Is there a case when an object is declared without a call to the constructor? There certainly is. All of them, in fact. Declaring an object variable creates a location ("address") into which the program can later write a reference ("pointer") to an instance of an object [of the specified Type]. Instantiating an object is the action that calls the ...


0

then why isn't ArrayList<Integer> grades; already implies ArrayList<Integer> grades = new ArrayList<Integer>(); First, read @AvivCohn's answer about references. Second, besides @Basilev's answer about dependency injection, there are also two reasons why an implicit call to new may not be desired (what follows are ...


1

Yes, references to objects can be initialized without constructors or visible assignments. This effect is often achieved by using Dependency Injection containers like Guice or Spring. class Foo { @Inject Integer bar; }; Foo foo = container.create(Foo.class); assert foo.bar != null; If this class is instantiated in Injection container context, its ...


6

Consider two distinct concepts: objects and references. References 'point' to objects in memory, but they are not the objects themselves. In Java, we always refer to and control objects through references to them. In Java, you never store the object itself in the variable, only it's address in memory (not exactly - implementation dependent - but you get the ...



Top 50 recent answers are included