简体   繁体   中英

Java different function return types

In a tutorial for java programming, I saw this example:

public ClassName open()
{
    code....
}

I'm a little bit confused to use the a class name. I'm used to create method like this:

public void open()
{
    code...
}

Could some explain? Thanks!

A void function returns nothing while the first example returns a ClassName object.

For example:

public int getInt() {
    return 1;
}

In this example an integer is returned. Therefore the method must define what type of object it will return.

Or in your case, what could make sense is the following:

public User getLoggedInUser() {
    User user = SomeDeviceClass.getLoggedInUser();
    return user;
}

Reading material: http://docs.oracle.com/javase/tutorial/java/javaOO/returnvalue.html

在该教程中,程序员必须返回类的任何对象,因为ClassName将是返回类型。

The return type of the function maybe the object of the class.

    public ClassName open()
    {
       ClassName obj;
.
.
.
.
       return obj;
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM