• Saves Valuable Time
  • Trusted Accuracy since 2004
  • 15-Day Money-Back Guarantee

Java Equivalent to VB 'ByRef' Parameters

Java does not support VB-style 'ByRef' parameters. All Java parameters are passed by value. However, you can wrap the parameter type in another type (we call it 'RefObject') which still allows changes internal to the object.

Here's a simple example:


Public Sub refParamMethod(ByRef i As Integer)
    i = 1
End Sub

Public Sub callRefParamMethod()
    Dim i As Integer = 0
End Sub


public void refParamMethod(tangible.RefObject<Integer> i)
    i.argValue = 1;

public void callRefParamMethod()
    int i = 0;
    tangible.RefObject<Integer> tempRef_i = new tangible.RefObject<Integer>(i);
    i = tempRef_i.argValue;

package tangible;

// Copyright © 2007 - 2020 Tangible Software Solutions, Inc.
// This class can be used by anyone provided that the copyright notice remains intact.
// This class is used to replicate the ability to pass arguments by reference in Java.
public final class RefObject<T>
    public T argValue;
    public RefObject(T refArg)
        argValue = refArg;

Use VB to Java Converter to convert from VB to Java.     

Additional resource: VB.NET and Java Equivalents

Copyright © 2004 – 2021 Tangible Software Solutions, Inc.