Hello,

I have two forms (Main, Login) when the user logs in and has the right email / password..
I would like it so I can display the username on the main screen..

I have a class and I'm setting the values inside the class however when I try to access the method it doesn't show up.. For example

(in the login form)

Account account = new Account();
account.setName("Phillip");

It would display Phillip in this form.. But when I try and do it in the main form..

(in the main form)

Account account = new Account();
account.getName();

It just displays as blank..

Can anyone help me please? :)

On the main form you receive the new account value created locally (empty / blank).

The Account account variable should be defined in the program class with public visibility, before the login form is launched.
Then the login form mut not define a local variable intead must use the public one from the program class to assign the account name.

In the main form, you must not set the Account account as new, because it has already been defined on the program class.

Hope this helps

Easy way to do what you want is to declare the methods, attributes and class as static (The Account class). Then you can access them without having to create an instance variable (the new Account() stuff).

So your code becomes Account.setName("Phillip"); and Account.getName(); .

This technique is best when there will only ever be one account used at a time.

Another thing to look at is the Singleton Pattern (which is generally considered a better way to do this than a static class).

Edited 5 Years Ago by Momerath: n/a

an other way of doing this is pass the name of user to the constructor of "main form"
from whereever you call main..... like when you press login you show main form just pass the name of user in construction like

MainForm(account.getname())

and in main form the constructor will be defined as

MainForm(String loginUserName) //set the name in header of the form
This article has been dead for over six months. Start a new discussion instead.