I read some lines in Effective Java: Programming Language Guide
Joshua Bloch and find out that I should avoid the usage of mutable objects. Because of I read the book I know how to make a mutable object immutable (e.g. usage of private and final modifier).
Well however I have a "dummy" data holder class with some private fields. Each field is accessable with a get method and also a corresponding set method. So because of this set methods objects of this class are not immutable.
The question is now how to avoid these set methods? Pass all (e.g. 20) parameters to the object constructor? I think this is not really good design because I have to keep care of the order of parameters, have to pass null references if I do not want to set a special parameter and so on.
So I think about following approach:
Create an interface with all get methods and let it implement from dummy data holder class
Create an abstract class with a private constructor and a static factory method which returns the "get" interface instance of the data holder object.
In the static factory method I configure the data holder object with all necessary set methods
Make the data holder class package private so that a object can only be instanciated over the static factory method which is defined in the abstract class
In the next step I store the configured and created data holder objects in a list.
What is the best approach to read out a object an modify the object although it is immutable? Create a new object with a static factory method which sets the new value internally and replace it with the object in the list?
As #NilsH pointed out: you should go for the Builder pattern, ideally based on a fluent interface.
As an example, you may look at make-it-easy.
Related
In a class, I want to define an empty object and use it anywhere we need it. This object needs to be immutable to avoid accidentally modification. If this object is defined as a public static final member of the class, the object could be changed if the class itself is mutable.
What's the good way to create an immutable object in a mutable class?
If you need to make a class immutable then you need to fulfill this requirements:
all its fields final
the class declared as final
the this reference is not allowed to escape during construction
Any fields which refer to mutable data objects are:
private
have no setter method
they are never directly returned of otherwise exposed to a caller
if they are changed internally in the class this change is not visible and has no effect outside of the class
If you cannot modify the class to make it immutable (final class with final fields, etc.) than just write yourself an immutable wrapper (proxy from GOF?) and use it instead. Inside it can has a delegate to your original class instance.
Whenever we call a constructor in Java, it creates a new object and returns its reference in the end (of newly created object).
Is there any possibility that a Java constructor does not create a new object but return the reference to an already created object?
// Is it possible that myObject is not a new object, its already existing object
MyClass myObject = new MyClass();
I have a list of objects of some class, and based on few parameters in constructor sometimes it more efficient that I don't create a new object, instead I pick up an already existing object. Is there is any other way?
No. Constructors by definition run when a new object is created to initialize it. If the constructor is run, a new object has already come into existence, and there's nothing you can do about it.
What you could do is make a static method which either creates a new object, or returns an existing one. This is the standard approach in such cases.
Say, Boolean.valueOf(boolean value) in the standard library exists for the purpose of avoiding creation of extra objects. You can create them using new Boolean(value), but it is much better to call this method because it will return the same object for the same values.
you cannot do this using constructors but you could use one of the patterns mentioned below.
If you only ever need 1 object then use the Singleton pattern.
If your might have a few variations then use Flyweight pattern as duffymo mentioned.
As duffymo mentions in his comment below - if you using any of these patterns then its important from a concurrency perspective to understand that these objects will be global state - you should therefore ensure they are immutable, and if you cannot make them immutable then you may want to rethink your approach.
No, this is not possible.
JLS section 15.9:
Unqualified class instance creation expressions begin with the keyword new.
An unqualified class instance creation expression may be used to create an instance of a class, regardless of whether the class is a top level (§7.6), member (§8.5, §9.5), local (§14.3), or anonymous class (§15.9.5).
and JLS section 12.5:
A new class instance is explicitly created when evaluation of a class instance creation expression (§15.9) causes a class to be instantiated.
...
Just before a reference to the newly created object is returned as the result, the indicated constructor is processed to initialize the new object using the following procedure: [...]
Notice that this clearly mentions creation of objects and not a possibe re-utilization.
On the other hand, you could create a static factory for your object that uses a pool. You could take a look at the implementation of Integer.valueOf(i) for example. Refer to this answer for example.
You cannot achieve this with just a constructor in Java.
If required, such a behaviour is achieved by using either static method inside the class (like Integer.valueOf(0)) or the whole dedicated object of the different class (like DocumentBuilderFactory) to return the instances. This provides enough control to substitute the existing object instead of always creating a new one.
As a rule, such objects should be immutable and thread safe to be easily shareable. Also, instance reuse and sometimes caching is implemented along these lines.
No. Class provides the blueprint for objects, when using the new operator it is followed by a call to a constructor, which initializes new object.
Source.
If you wish to reuse objects for any reasons you may want considering implement the Flyweight pattern as well as the Factory pattern into your project for best result.
No it's not possible. Create a static method to create objects based on required logic and don't forget to make constructor private.
You have some object that just contains static data and static methods. Lets call it DataHoldingClass. You want this because the data is then easily accessible in code by writing object.VALUE and object.method(). It's all just for laziness and simplicity. And we all agree that this is important.
But some of the static method()s are common in your code, so you want to share them among DataHoldingClasses. The OOP approach is to define some abstract class BaseClass, put common methods there and then let the DataHoldingClass extends BaseClass.
But it's not possible, because each DataHoldingClass has different values and you cannot reach that values from abstract class. If you define public static final String VALUE in abstract, all subclasses would have only one shared VALUE and if you define separate VALUE in each DataHoldingClasses, you cannot create static String method() { return NAME; } in abstract class, because NAME isn't defined in abstract yet.
The only way to make it work is create values and methods non-static. Then the methods will be inherited from abstract class BaseClass and will work with DataHoldingClass specific values. But now you have to create new class instance every time you want access the data. Hmm, we've advanced from splash to mud.
But we can solve it, right? Just create some object to hold instances or make the DataHoldingClass a singleton. But this does not seem to be much better.
Another way might be to pass values to static methods by parameter, but calling some object method with the same object values as parameter seem weird and remember, we do this because of laziness, so we don't want to write object name two times.
Whats your way to storing such data objects? Currently, I'm using singletons, but I feel there must be some better way. Any idea?
Edit
example of usage:
Lets create such DataHoldingClass for each table in our database. Each table has own constants like COLUMN_ID = "_id" and TABLE_NAME = "evilTable" and every table have methods like insert() or update(). The methods are same for every table but can be overriden for some tables. The Constants are different. And I want define it as constants, because it is then easily accessible like Table.TABLE_NAME etc.
In Java, does the constructor of a class create an instance of that class? And if it does, does it also initialize the variables of that class?
Constructor doesn’t create the instance of the Class.
Instance creation is done using either:
1.Using Class.forName()
2.ClassLoader loadClass()
3.Using clone()
4.Deserialization
5.Using reflection
6.new keyword
Constructor in java is a special type of method that is used to initialize the object.
Java constructor is invoked at the time of object creation. It constructs the values i.e. provides data for the object that is why it is known as constructor.
Rules for creating java constructor
There are basically two rules defined for the constructor.
1.Constructor name must be same as its class name
2.Constructor must have no explicit return type
Types of java constructors
There are two types of constructors:
1.Default constructor (no-arg constructor)
2.Parameterized constructor
Constructors don't create objects. They merely initialize objects(and their data members) once they are created using parameters(when provided) or to default values.
When you create an instance of the class using new operator, the constructor of the class is called so as to initialize the instance variables.
If the constructor defined is default, then instance variables have to be assigned to the newly created object explicitly.
However when you override a constructor using fields, then the instance variables for that newly created object are assigned during object creation.
I would love to explain this in a very simple language. In the real-world to build something, we need two things first is its prototype/model, and the second is someone who can create it based on that prototype.
A very relevant simple example is to build a house, you first need its blueprint(map), then a constructor who can build it based on that blueprint. So, similarly in the programming language
Object: A real-world entity for which we create a class.
Class: A class describes the "blueprint" of the objects that are made out of it (are instances of it).
For software development, we first have to think about the objects(any real-world entity), then we create a class (blueprint) for it, which contains its attributes.
After creating a class when we need to create one or more objects based on it, for this, we need a constructor to build it.
Whenever we create a new object, we have to use new keyword, and it tells the constructor to create the object.
When you are initialing variables in a class they are just part of the blueprint, based on that, the object will be created. So, without a constructor, you cannot create new objects, but there are some exceptional cases and tricks where you can create them without calling constructors.
I want to write a class in java that will only allow creating its object from another class
Lets say I have the following
public Class Zoo
{
private Map<String, Animal>
....
getOrCreateAnimal(String name);
}
I want to limit my class users to create Animal class only using Zoo.getAnimal() So they will be signed in the map.
Is there a way to limit the - new Animal() constructor.
I can probably do it using protected. Is there another way?
The standard way to do this is to make your constructor private. Sometimes you might use protected instead but in general private works better. You will need to arrange things so that Zoo gets access to the constructor, for example by making Animal an inner class of Zoo.
This means that the only way to create an instance is through your factory method.
The easiest way to handle this sort of restricted factory access is through putting the factory method in the class itself.
So have a factory method in the Animal class but then have that call the relevant methods in Zoo.
Make your constructor as private and create objects only in your getOrCreateAnimal method and while creating the object add it in the Map.
Use ConcurrentHashMap to make your implementation scalably - thread safe.