0

I'm using JFrame and I'm wondering why int 'a' doesn't update. If I run this program this comes out:: 10 30 10

I want to get

10 30 30

But I cant change my method initialize or initialize2 to a static method because that's not possible while using JFrame. In the example it looks like you can, but in my actual program you can't. Anyone got any ideas? So my question is, how can I update my int a? Thank you.

import java.awt.EventQueue;

import javax.swing.JFrame;
import javax.swing.JButton;
import javax.swing.JSpinner;
import javax.swing.SpinnerNumberModel;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;


public class test {

    private JFrame frame;

    /**
     * Launch the application.
     */
    public static void main(String[] args) {

        EventQueue.invokeLater(new Runnable() {
            public void run() {
                try {
                    test window = new test();
                    window.frame.setVisible(true);
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    /**
     * Create the application.
     */
    public test() {
        int a = 10;
        initialize(a);
        initialize2(a);
    }

    /**
     * Initialize the contents of the frame.
     * @param a 
     */
    public int initialize(int a) {
        frame = new JFrame();
        frame.setBounds(100, 100, 450, 300);
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.getContentPane().setLayout(null);

        System.out.println(a);

        a = a + 20;

        System.out.println(a);

        return a;


    }
    public void initialize2(int a){

        System.out.println(a);

    }
}

3 Answers3

3

Primitives are value types. You have defined a within the constructor and it is only local to that scope. The a inside initialize is a different integer entirely and is only local to the scope of that function. Hence, when you modify it, you are modifying a variable local to the scope of intitialize and not the one in test.

Either make a a property of your class, or return the value of a that you want from initialize:

public class test {

    private int a;

    ...

}

or

public class test {

    private test() {
        int a = initialize(a);

        ...
    }

    public void initialize(int a) {
        ...
        return a;
    }    
}

Depends on whether you want other methods to do things with a later though. In that case making it a property of the class is better. A note on naming conventions. Classes are typically named with uppercase letters, so your class should be called Test instead of test.

An important thing to note here is that even if you passed in Integer, this still wouldn't work because Java is a pass-by-value language, and even references are passed by values. Hence, you cannot "unseat" a reference and reassign it to something else. But what you can do is invoke methods on the reference or change public properties. This is because even though it is pass by value, the reference is still pointing to the same object as the one in the caller and so you are essentially still modifying the underlying object. This is a subtlety that tends to trip people up sometimes. That said, you still can't do that with Integer (or any of the other autoboxed equivalents of primitive types) or String, since those are immutable objects.

Vivin Paliath
  • 87,975
  • 37
  • 202
  • 284
2

Java is Pass by Value - in other words when initialize returns, the value of a in that scope will not have changed. In order for the value to change, you must set the value within that scope. This can be done by having initialize return the new value, and set a in that scope to the returned value:

int a = 10;
a = intialize(a);

public int initialize(int a){
    ...
    return a;
}
Community
  • 1
  • 1
copeg
  • 8,092
  • 17
  • 27
0

You are passing parameter as "a" and value of a is 10 in constructor. Value of a is not getting changed in test().

You can declare variable at class level OR You can do one thing change the return type of initialize method to int and pass returned value of method initialize to method initialize2 instead of a. Something like this::

public test() 
{
int a = 10; 
int b = initialize(a); 
initialize2(b); 
} 

public int initialize(int a) 
{ 
frame = new JFrame();
frame.setBounds(100, 100, 450, 300);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
 frame.getContentPane().setLayout(null); 
System.out.println(a);
a = a + 20; 
System.out.println(a);
return a;
} 

public void initialize2(int b){ 
System.out.println(b);}
Kaushik
  • 59
  • 5