I am a JAVA newbie and have tried to find the answer but have struggled because I don't really know how to use the correct terminology, and the amount written on Java is huge. So my apologies in advance if there is a reference somewhere that explains my question but I haven't been able to find it.
I have a question about defining user classes in JAVA GUIs. I want to write a GUI that uses my objects, one those objects are defined they essentially hold some number, the result of previous actions on the object.
However, in trying to get there I have refined the problem to trying to understand how to define the classes in one place and act on them in another.
For example ...
public class Form extends javax.swing.JFrame {
/**
* Creates new form Form
*/
public TheOtherClass otherClass;
public Form() {
TheOtherClass otherClass = new TheOtherClass();
}
}
I thought that if you declared the class as an instance variable, and then instantiated it in the constructor the object would "live" while the forms lives. However, when I try to use the object I anywhere else I get a NullPointerException.
I'm thinking this is because although the object is declared at the class level it is created in the constructor and is destroyed when the constructor code finishes. Is this correct ?
When I do this it works ...
public class Form extends javax.swing.JFrame {
/**
* Creates new form Form
*/
TheOtherClass otherClass = new TheOtherClass();
public Form() { }
}
Is this OK ? is there a better or preferred way ? Can someone please direct me to a reference or a tutorial that might help ?
Cheers P
Both methods are fine, it's a matter of preference for your specific example. However, you're not setting the member variable of the class when you do this:
public Form() {
TheOtherClass otherClass = new TheOtherClass();
}
You are creating a new variable with the same name. Change it to:
public Form() {
this.otherClass = new TheOtherClass();
}
otherClass is an instance variable and it becomes immediately available as soon as it is defined. In the first code block, however you declare it but you do not instantiate it, so Java sets its value to its default: null, for all not primitive types). In the class constructor you do not instatiate it, but you instatiate another variable with the same name whose scope is within the constructor, as pointed out by @nickb.
To avoid this null when instantiating a Form you have 3 ways in Java:
1) Declare and instantiate immediately (your second code block)
public class Form extends javax.swing.JFrame {
TheOtherClass otherClass = new TheOtherClass();
}
2) Instantiate it in a instance initializer block
public class Form extends javax.swing.JFrame {
TheOtherClass otherClass;
{ otherClass = new TheOtherClass(); } //instance initializer block
}
3) Instantiate it in the constructor
public class Form extends javax.swing.JFrame {
TheOtherClass otherClass;
public Form() {
otherClass = new TheOtherClass();
}
}
During the instantiation of a Form object, Java will go through the 3 points in order.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With