简体   繁体   中英

Returning a value from a void method in Java

I have a method :

public void dbQuery(String query, String what) {
    try {
        Statement stmt = conn.createStatement();
        ResultSet rs = stmt.executeQuery(query);

        if(what.equals("temp_table")) {
            String temporary_table = rs.getString("table_name_temp");
            System.out.println(temporary_table);
            return;
        }

        }

    catch (Exception e) {
        e.printStackTrace();
        }

    finally {
        if (rs != null) rs.close();
        if (stmt != null) stmt.close();
        }

}



String query = "EXEC temptables.dbo.create_temp_cdr 'SIP'";
String temporary_table = db.dbQuery(query,"temp_table");

How do I get the return of a void to use it in another db.dbQuery() ?

PS : I need a value from dbQuery() so I can construct another query to call dbQuery() again

How do I get the return of a void to use it in another db.dbQuery() ?

What do you mean by "the return of a void"? If you mean "the return value" then there isn't one - that's the point of the method being declared as void .

If you want to return a value, then don't make it a void method... given this:

String temporary_table = db.dbQuery(query,"temp_table");

it looks like you just want to change dbQuery to return String . Note that you'll need to work out what to return if what isn't temp_table . (You should also fix your exception handling - just printing out the stack trace to stdout and then continuing regardless is almost never the right approach.)

You can either have your method not be void, and have a return type, then you can have the line

return temporary_table;

which would return the temporary_table variable.

Another way would be to pass by reference to the method. For example, you can pass a StringBuilder object to the method. Any changes to this object in the method will then also apply after the method has returned.

public void addToString(StringBuilder query, String toAdd) {
    query.append(toAdd);
}

A call to this method of

StringBuilder query = new StringBuilder("start");
System.out.println(query.toString());
addToString(query, "end");
System.out.println(query.toString());

would have the output of:

start
startend

You can't return a value from a method that is defined as void - by definition this means it doesn't return any value.

If you see a return; statement in a void method, it basically means that this will end the current method and return processing back to the caller.

I'd make your method string instead of bool. Void won't return anything ever.

A void method is something that does not return anything. But, your requirement seems like you have to use value generated in this method in the previous layer. One obvious solution for this is to change it's return type from void to String. But, if you don't want to do this , simply pass one more string parameter to your method. String is an object, so when you are passing it to a method, you are actually passing reference of that object to it. Simply set your generated value in this string and you will be able to access it in previous layer with that string variable(It's sort of similar to Pass by Reference in C). I hope this workaround will solve your problem. Please correct me if I am wrong.

In fact he wants to have something similar like functions in VB or so. First, create your void as usual.

private  Void getheightoflistview(ListView nameOfListView){
   //do whatever you want to do
    int numberOfItems = nameOfListView.getCount();
    //You want to return the numberOfItems 
}

So if you want to return an Integer value, just replace the Void with Integer

private  Integer getheightoflistview(ListView nameOfListView){
   //do whatever you want to do
    int numberOfItems = nameOfListView.getCount();
    //You want to return the numberOfItems 
    return numberOfItems; //this is the Integer you want to return

}

and add the return value

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