Home > OS >  What does a return type of class return?
What does a return type of class return?

Time:09-28

Suppose I have a class called Practice.

public class Practice 
{
    public static void main(String[] args) 
    {
        test x= new test();
        System.out.println(x.testFunction());
    }

}   

And another class called Test

class Test 
{
    public Practice testFunction()
    {
        return xxxx;
    }
}

So since the return type is of class Practice, what could I replace xxxx with. Like suppose the return type is int, so I am supposed to return a number, when the return type is string, I am supposed to return a sentence. So when the return type is a class, what does it actually return.

CodePudding user response:

The return type would be an Object of type Practice. Or in other words, an instance of the class Practice.

Normally, you would create such an instance by using a constructor.

E.g. using the default constructor: return new Practice();

CodePudding user response:

Ideally an object of that specific class like this :

class Test 
{
    public Practice testFunction()
    {
        return new Practice();
    }
}
  •  Tags:  
  • java
  • Related