Reputation: 1987
public class welcomepage extends javax.swing.JFrame {
backendcode bec;
String username;
public welcomepage() {
initComponents();
username=null;
backendcode bec= new backendcode("dummy");
System.out.println("bec created "+ bec);
}
private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {
System.out.println("bec created "+ bec);
bec.back_login_credentials(username);
}
/*
and other private methods(not mentioned here) which also needs to access bec object
*/
}
public class backendcode {
public backendcode(String dummy) {
//some code
}
public void back_login_credentials(String username, String password) {
//some code
}
}
i have declared bec(backendcode object so that it is visible to entire class)as member data inside welcomepage class and initialised in its construcor but it gets created inside that construcor with some intialisations but bec object's value inside jButton1ActionPerformed method will be null. why is it.. what is the solution to get that intialised object instance??
Upvotes: 0
Views: 111
Reputation: 72294
In your constructor you're doing:
backendcode bec= new backendcode("dummy");
...which creates a new variable. You want to set the existing field like this:
bec= new backendcode("dummy");
Whenever you specify the type, you're creating a new variable rather than setting the value of an existing one. It can be confusing, since as you've found local variables can have the same name as fields - when they do, it's called shadowing and it's the local variable that's referenced by default for the remainder of that method, rather than the field.
You can explicitly reference the field, rather than the local variable by using the this
keyword. For instance, if you changed your constructor to the following:
public welcomepage() {
initComponents();
username=null;
backendcode bec= new backendcode("dummy");
System.out.println("bec created "+ this.bec);
}
...you would see that the field (this.bec
) is still null.
Incidentally, you should probably declare the field as private - that way it will still be visible to the entire class (that's why it's a field) but will not be visible (or more importantly, changeable) from any other class. If you do need other classes to change the state of the field, then you can then do that in a more controlled manner by way of getter / setter methods.
Upvotes: 1
Reputation: 1541
The global bec
variable is never initialized. You create a new bec
variable in the scope of your constructor.
Try the following:
public class welcomepage extends javax.swing.JFrame {
backendcode bec;
String username;
public welcomepage() {
initComponents();
username=null;
bec= new backendcode("dummy");
...
Upvotes: 3
Reputation: 15552
You are hiding the field with this line in your construtor.
backendcode bec= new backendcode("dummy");
It should be
bec= new backendcode("dummy");
What you have done is called shadowing
I would also recommend looking up Java naming conventions. It will make your code easier to read.
Upvotes: 0
Reputation: 25950
Change this line
backendcode bec = new backendcode("dummy");
to this line:
bec = new backendcode("dummy");
You are hiding the global variable bec, if you look at your code carefully.
Upvotes: 1