• CONTEST ALERT - Experience the power of DDR5 memory with Kingston Click for details

Java Query - Apologise for not asking in query thread

Not open for further replies.


I don't want IT
Date d = new Date();
using this statement we can see that d has got some value and can be printed. I want to make a similar class, which will assign a value to the object and not create members within the object.
Class cls{

so when I write
cls cmember = new cls();
I can print SOPln(c); and not SOPln(c.<xyz>);
My Ideas:
Returning a value from constructor is not possible.
Can I know cmember from from cls, ie can I knw what variable name was used to Initialize a class?


you can do one thing..

Initialize the member variable of your cls class in constructor. Then implement "toString" method in cls class which simply prints all the member of cls class in a format. And then you can do..

Cls c = new Cls();

This will surely solve your problem..

Btw when you directly concat any class to with string or pass the any class in string argument, then java will call toString method of that class..
Not open for further replies.
Top Bottom