+ 5

Is there any way to return a object from a method in java?

how do people return obj by using methods that have a return type?

22nd Apr 2018, 11:56 AM
Leon Yang
Leon Yang - avatar
6 odpowiedzi
+ 8
You just declare the class name as return type for tthe method, e.g. String return type: public String myMethod(){ // your code String s = "Hello world!"; return s; } Could be shortened for String, but that's how it works for all classes.
22nd Apr 2018, 12:02 PM
Tashi N
Tashi N - avatar
+ 4
Tashi N please i also want to know this,your explanation is quite hard. Donna nice,but this function always has to belong to the class? cant a function belonging to No-class return an object by using a class?
22nd Apr 2018, 12:05 PM
᠌᠌Code X
᠌᠌Code X - avatar
+ 3
tashi n i ment that if I create an 2 classes and one gets the obj of the other
22nd Apr 2018, 12:08 PM
Leon Yang
Leon Yang - avatar
+ 3
like this class Program { } class hi{ static Obj ob={ Hello Bob=new Hello(); return hello; } } class hello extends hi{ any way to return an object?
22nd Apr 2018, 12:11 PM
Leon Yang
Leon Yang - avatar
+ 2
we also can do this.. with the help of.. changing in return type of function .. like as you very well know about local class.. we can return the reference id... by using it
22nd Apr 2018, 2:47 PM
Arun Tomar
Arun Tomar - avatar
+ 2
public String getName() { return "Your Name" ; } String name = obj.getName(); Method getName() returns String object.
25th Apr 2018, 7:06 AM
Parveen D
Parveen D - avatar