My new dilemma, why these variables keeps getting back to their default state even though I'm initializing them?

Hello fellow coders, newbie here!

Here’s my problem, I made a class other than my Main class and named it “Second” and you can see the codes I wrote down below:

package com.company;

public class Second {

    public int x;
    public int y;
    public int a;

    public void summary(int x , int y){
        this.x = x;
        this.y = y;
        a = x*y;
    }
}

and in the Main class I wrote:

package com.company;

public class Main {

    public static void main(String[] args) {
        Second second = new Second();
        second.summary(5, 7);
    System.out.println(second.a);
    }
}

and result is “0” every time. basically “null” but for integer.

In my logic what I’m doing is to call the “summary” method from the “Second” class and initializing “x” & “y”, and since I implemented an argument in said method which also initialize the “a” variable, I was expecting to see “35” as output, but needless to say that didn’t happen!

I really appreciate it if someone could help me understand what’s going on here and why is this happening. thanks!

Weird! Because I copied/pasted your code into this repl and I got 35! (with the minor adjustment that I had to remove the public keyword from class Second)

Hi there, I have tried copied/pasted and run the code. It works for me without any modification, but I don’t think is good to write the int a as public variable and called in Main class for this scenario, unless u r testing out something. :slight_smile: