+ 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?
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.
+ 3
tashi n
i ment that if I create an 2 classes
and one gets the obj of the other
+ 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?
+ 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
+ 2
public String getName() {
return "Your Name" ;
}
String name = obj.getName();
Method getName() returns String object.