Untitled

Anonymous
Plain Text
10/15/2025 9:34 AM
786 B
17
Indexable
Never
class SwapExample {
    int value;

    
    SwapExample(int v) {
        this.value = v; 
    }

    void swap(SwapExample obj) {
        int temp = this.value;
        this.value = obj.value;
        obj.value = temp;
    }

    public static void main(String[] args) {
        SwapExample obj1 = new SwapExample(10);
        SwapExample obj2 = new SwapExample(20);

        System.out.println("Before swapping:");
        System.out.println("Object 1 value = " + obj1.value);
        System.out.println("Object 2 value = " + obj2.value);

        obj1.swap(obj2);

        System.out.println("\nAfter swapping:");
        System.out.println("Object 1 value = " + obj1.value);
        System.out.println("Object 2 value = " + obj2.value);
    }
}

    
Editor is loading...
Leave a Comment