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

Java Query - Apologise for not asking in query thread

Status
Not open for further replies.

DizitalNovice

I don't want IT
Date d = new Date();
System.out.println(d);
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.
Say,
Class cls{
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?
 

fabler

Journeyman
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();
System.out.println(c);

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..
 
Status
Not open for further replies.
Top Bottom