Java Complete Tutorial Ep. 23 - Returning Objects

preview_player
Показать описание
In this episode of the Java Tutorial, I show you how to return objects inside of your methods.
Like and subscribe!! 😍

Stay Connected and Updated!! 👁
More Videos coming soon.
Leave a comment for any future video suggestions.
Рекомендации по теме
Комментарии
Автор

Whoever that is interested to understand Kody better please see the example below.

A method can return any type of data, including class types that you create. For example, in
the following program, the incrByTen( ) method returns an object in which the value of a is
ten greater than it is in the invoking object.
// Returning an object.
class Test {
int a;
Test(int i) {
a = i;
}
Test incrByTen() {
Test temp = new Test(a+10);
return temp;
}

}
class RetOb {
public static void main(String args[]) {
Test ob1 = new Test(2);
Test ob2;
ob2 = ob1.incrByTen();
System.out.println("ob1.a: " + ob1.a);
System.out.println("ob2.a: " + ob2.a);
ob2 = ob2.incrByTen();
System.out.println("ob2.a after second increase: "
+ ob2.a);
}
}
The output generated by this program is shown here:
ob1.a: 2
ob2.a: 12
ob2.a after second increase: 22

As you can see, each time incrByTen( ) is invoked, a new object is created, and a reference
to it is returned to the calling routine.
The preceding program makes another important point: Since all objects are dynamically
allocated using new, you don’t need to worry about an object going out-of-scope because the
method in which it was created terminates. The object will continue to exist as long as there is
a reference to it somewhere in your program. When there are no references to it, the object will
be reclaimed the next time garbage collection takes place.

spenceremmanuel
Автор

You helped me more than the university.

ksaykov
Автор

3:35 We can just return a new object and we dont need to create a temporary one:
return new Number(this.number * this.number);

speedflash
Автор

Im confused on how the number is 5 ? Where is 5 assigned ?

Blckops
Автор

i just can't understand what's going on. I want to die

romanmendelson
join shbcf.ru