Is there a way to initialize an object without a constructor?

So in java when initializing a String you don't need to make use of a constructor, you just put in the literal but you can use the constructor if you please since String is a class. Now my question is is there any way to initialize a user-made class without the constructor?

8/15/2019 2:14:20 PM

Ibrahim Adeosun

8 Answers

New Answer


Well, Java String class is special because it is associated to how string literals are stored in the string pool. https://stackoverflow.com/questions/50302985/how-does-java-initialize-string-literal Based on some of the answers there, it seems like the assignment of a string literal to the String class also involves the calling of one of the constructors in the class, so technically it would be impossible to do classes without constructors, as constructors are always called when an object is initialized, even if it was designed to do nothing.


Maybe there are some unrecommended ways, but if you meant by that you don't want to define a constructor, then I can say that classes have a default constructor by default, which is just used to create the object self.


If I initialise member variables outside the constructor like this, is that considered as initialising an object without a constructor? ... No? https://code.sololearn.com/c7j319avp4Dd/?ref=app


Well, as a matter of fact, when you assign a literal to a string, you assign the memory address (heap) of the literal from the string pool to the String variable. It doesn't mean you do an intanciation. Keep in mind that String is immutable. You do the same with object that you assign to a class literal. String myString = ''Hello''; maClasse m1 = new maClasse(); maClasse m2 = m1;


No, I don't see any solution of it near me because object has to assign with constructor to initialized but good question to ask.


When you create a class, Java uses a default constructor if you do not define your own constructor. Even if you don't have a constructor in your class, you can still create objects. Example: class A{ } class B{ public static void main(String[] args){ A x = new A(); B y = new B(); //both are valid and the code will compile. Try running them to see the result. } }


String or text stored in string pool and doesn't need to make an object or constructor for strings but other classes and type are diffrent


Primitive la third _ answer please