Nathan
Nathan

Reputation: 1425

How can I declare a private method in a public method in Java?

Fairly new to Java, so I hope the wording makes sense.

I have some Java code that needs to be called a few times within a public method, how can I reuse that code by placing it inside the public method and calling it?

Maybe I am making things hard for myself, so here's the code sample.

public static final String parseText( final String text )
{
    StringBuffer parsedText = new StringBuffer();

    private static void appendParsedText( String snippet )
    {
        if ( parsedText.length() > 0 )
        {
            parsedText.append( ", " + snippet );
        }
        parsedText.append( snippet );
    }

    if ( text.contains( "string1" ) )
    {
        parsedText.appendParsedText("string1");
    }

    if ( text.contains( "string2" ) )
    {
        parsedText.appendParsedText("string2");
    }

    if ( text.contains( "string3" ) )
    {
        parsedText.appendParsedText("string3");
    }

    return parsedText.toString();
}

Although the code is invalid, hopefully it makes sense with what I'm trying to achieve. I know there's a join() method in Apache Commons StringUtils but it seems like overkill, this method is the only place where this needs to happen.

Upvotes: 0

Views: 1547

Answers (2)

Nathan
Nathan

Reputation: 1425

I would assume that the following works, but I'm defining a method in my class that is only being used in one other method of the class. To me this seems a little wasteful.

private static final StringBuffer appendParsedText( StringBuffer parsedText, String snippet )
{
    if ( parsedText.length() > 0 )
    {
        return parsedText.append( ", " + snippet );
    }
    return parsedText.append( snippet );
}

public static final String parseAccess( final String text )
{
    StringBuffer parsedText = new StringBuffer();

    if ( text.contains( "string1" ) )
    {
        parsedText = appendParsedText( parsedText, "string1" );
    }

    if ( text.contains( "string2" ) )
    {
        parsedText = appendParsedText( parsedText, "string2" );
    }

    if ( text.contains( "string3" ) )
    {
        parsedText = appendParsedText( parsedText, "string3" );
    }

    return parsedText.toString();
}

Upvotes: 0

Dan D.
Dan D.

Reputation: 32391

As you said, the code is not compiling as Java doesn't support such a construct. I would suggest you define the parseText() and appendParsedText() separately and just call the private method from the public one.

So it would look like this:

public static final String parseText( final String text ) {
    return appendParsedText(text);
}

private static String appendParsedText( String snippet ) {
    StringBuffer parsedText = new StringBuffer();
    if ( parsedText.length() > 0 ) {
        parsedText.append( ", " + snippet );
    }
    parsedText.append( snippet );

    if ( text.contains( "string1" ) ) {
        accessTypes.appendAccessTypes("string1");
    }

    if ( text.contains( "string2" ) ) {
        accessTypes.appendAccessTypes("string2");
    }

    if ( text.contains( "string3" ) ) {
        accessTypes.appendAccessTypes("string3");
    }

    return parsedText.toString();
}

Upvotes: 1

Related Questions