0

I have a JFrame and a bunch of JTextFormattedField fields. The initialization for each JTextFormattedField is the same, so I'd like to have a private method that I call for each JTextFormattedField. This is what I tried to do:

JFrame Initialization:

myJFrame.getContentPane().add(addCharTextField(m_textField1, new textFieldFocusListener()), "cell 3 1 2 1,growx");
myJFrame.getContentPane().add(addCharTextField(m_textField2, new textFieldFocusListener()), "cell 3 2 2 1,growx");
myJFrame.getContentPane().add(addCharTextField(m_textField3, new textFieldFocusListener()), "cell 3 3 2 1,growx");

And the text field initialization method:

private JFormattedTextField addCharTextField (JFormattedTextField textField, FocusAdapter focusListener) {
    textField = new JFormattedTextField();
    textField.addFocusListener(focusListener);
    textField.setEditable(false);
    textField.setColumns(10);
    return textField;
}

I think there's might be problem with allocating the member variable after being passed to another method. Later on in my program when I try to access m_textField1, I get a NullPointerException. Does the garbage collector delete the JFormattedTextField at the end of addCharTextField? Is there a way around this, besides allocating the JFormattedTextField back in the JFrame initialization routine? Even if just for aesthetic purposes, I really wanted the initialization for each member variable in the JFrame initialization method to take up just one line of code.

Edit in response to this question being regarded as a duplicate: My questions are: "Does the garbage collector delete the JFormattedTextField at the end of addCharTextField?" (The answer was yes) And "Is there a way around this, besides allocating the JFormattedTextField back in the JFrame initialization routine?" (again yes, but ugly). The chosen answer in the other question that this one is regarded as a duplicate does not answer either of those. Now after digging into some of the other answers, and reading the comments here, I was finally able to piece together what's going on in Java. But that does not make this question a duplicate.

For what it's worth, the problem is a fundamental difference in the C++ 'new' and the Java 'new' that was confusing me. Considering how much other syntax Java borrowed from C++ without big differences in usage, It took me a bit to wrap my head around around the differences between the 'new' usage.

T Sloane
  • 326
  • 1
  • 2
  • 11
  • 1
    Java does **not** have pointers. –  Jan 26 '15 at 21:30
  • 1
    terseness has no value in and of itself, correctness and explicitness does –  Jan 26 '15 at 21:32
  • 3
    @JarrodRoberson From the [JLS](http://docs.oracle.com/javase/specs/jls/se8/html/jls-4.html#jls-4.3): 'The reference values (often just references) are pointers to these objects, and a special null reference, which refers to no object.' And think about `NullPointerException.` – user207421 Jan 26 '15 at 21:34
  • 2
    I'm with EJP here. Just because Java chose the name "reference" doesn't change what it is. Java's references are arguably more like C++ pointers than like C++ references. – Marko Topolnik Jan 26 '15 at 21:36
  • @MarkoTopolink And in fact they are essentially identical to Pascal pointers. – user207421 Jan 26 '15 at 21:37
  • 1
    Yet, several times when I've referred to them as "pointers" the "authorities" here have beaten up on me. But, more importantly, you don't allocate pointers/references. You allocate the things containing them and you allocate the things they reference, but you don't actually allocate them, as a distinct operation. – Hot Licks Jan 26 '15 at 21:45
  • you can not do "pointer" operations or math on variable names/references in Java. They are **not** pointers to in the classical sense in that they do not point to raw memory locations and the like. The way they are being referred to incorrectly in this question is the basis of the incorrect assumptions made specifically because of the mixed semantic. The use of the word "pointer" in the quoted JLS is not inferring the semantic of a *pointer* to memory, it is inferring the semantic of pointing your *finger* at something. –  Jan 26 '15 at 21:46
  • 1
    @JarrodRoberson Irrelevant. You're talking C or C++ now. The Java Language Specification uses the term 'pointer'. Ergo it has pointers. Not C pointers, but pointers. NB C isn't 'classic' to everybody. Some of us were using pointers before C appeared. Your sentence about the 'basic assumption' is simply untrue. The same code would fail in C, for the same reason: no pass-by-reference. Your final sentence is mere assertion. – user207421 Jan 26 '15 at 21:50

1 Answers1

2

No. Java is pass by value. The change you make to the passed argument is not applied in the original parameter.

user207421
  • 305,947
  • 44
  • 307
  • 483