Class name as return type

Help and support on OCA OCP Java Programmer Certification Questions
1Z0-808, 1Z0-809, 1Z0-815, 1Z0-816, 1Z0-817

Moderator: admin

Post Reply
OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Class name as return type

Post by OCAJO1 »

Yes, I found another odd code in the blogs while I was looking for examples of garbage collection,

Code: Select all

public class TestCode {
    
    public TestCode myMethod(){
    
        TestCode tc1 = new TestCode();
        TesdtCode tc2 = new TestCode();
        return tc1;    
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String[] args) {
        
        
        TestCode tc3 = new TestCode().myMethod();
What would be the point of having return type of a method be the of type of its own Class?
( I tried to find something about this type of return in javaDocs, but I didn't see anything more than a here it is!)


Thanks

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: Class name as return type

Post by OCAJO1 »

Still can't find anything out there that gives a meaningful answer the question!

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Class name as return type

Post by admin »

From the first page of google search
https://softwareengineering.stackexchan ... ing-itself
If you like our products and services, please help us by posting your review here.

OCAJO1
Posts: 221
Joined: Mon Nov 26, 2018 2:43 pm
Contact:

Re: Class name as return type

Post by OCAJO1 »

Ohhh...I was searching for the wrong thing. I should have searched for methods returning an instance of an object as the class passed in. Searching this way I also found something called a singleton design pattern that seem to be an example of a method with return type of its class. Is this something worth reading more about? Thanks
Last edited by OCAJO1 on Thu May 23, 2019 11:32 am, edited 1 time in total.

admin
Site Admin
Posts: 10058
Joined: Fri Sep 10, 2010 9:26 pm
Contact:

Re: Class name as return type

Post by admin »

For exam, no. In general, yes.
If you like our products and services, please help us by posting your review here.

Post Reply

Who is online

Users browsing this forum: No registered users and 56 guests