Page 1 of 1

Class name as return type

Posted: Wed May 15, 2019 6:57 pm
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

Re: Class name as return type

Posted: Wed May 22, 2019 12:23 pm
by OCAJO1
Still can't find anything out there that gives a meaningful answer the question!

Re: Class name as return type

Posted: Wed May 22, 2019 12:55 pm
by admin
From the first page of google search
https://softwareengineering.stackexchan ... ing-itself

Re: Class name as return type

Posted: Wed May 22, 2019 3:05 pm
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

Re: Class name as return type

Posted: Wed May 22, 2019 9:55 pm
by admin
For exam, no. In general, yes.