• Post Reply Bookmark Topic Watch Topic
  • New Topic

Why doesn't my compiler recognize my reference variables?  RSS feed

 
Eric Larsen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In the following class, javac does not compile because it doesn't recognize the reference variables "ui" or "loader"
*NOTE* This is a mock up for a user authentication system, please ignore the practicality issues and focus on the compiler errors

Also, I put in the this.promptUsername() and this.promptPassword() because the compiler wouldn't let me use login.promptUsername() and login.promptPassword() Why is that?

==============================================================================

public class Login {

boolean isValidLogin = false;

public static void main (String[] args) {

Login login = new Login();

login.go();

}

public void go() {

UserInterface ui = new UserInterface();

AccountLoader loader = new AccountLoader();

this.promptUsername();

this.promptPassword();

loader.loadAccounts("list.txt");

isValidLogin = loader.validateLogin (loader.getUsername (), loader.getPassword ());

if (isValidLogin == true) {

System.out.println ("Welcome, " + loader.getUsername() + ".");

} else {

System.out.println ("Invalid login. Please contact system administrator.");

}

}

public void promptUsername(){

loader.setUsername (ui.recieveUserInput("Username:"));

if (loader.getUsername() == null){

System.out.println("Please enter a valid username.");
this.promptUsername();

}

}


public void promptPassword(){

loader.setPassword (ui.recieveUserInput("Password:"));

if (loader.getPassword() == null){

System.out.println("Please enter a valid password.");
this.promptPassword();
}

}

}

==============================================================================

Attached is a screenshot of the errors I got.

javacErrors.jpg
[Thumbnail for javacErrors.jpg]
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator

The only "ui" and "loader" declarations that I see are local variables of the go() method. Those variables are out of scope in the promptPassword() method.

Henry
 
Eric Larsen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Henry Wong wrote:
The only "ui" and "loader" declarations that I see are local variables of the go() method. Those variables are out of scope in the promptPassword() method.

Henry


OMG.... I am an idiot. Thanks!
 
Steve Luke
Bartender
Posts: 4181
22
IntelliJ IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This is a problem of scope. A variable only exists in the block in which it is defined. A block is the surrounding brackets { ... }. So the variable login only exists in the main method - since it was declared inside the brackets for the main method:


Also, I put in the this.promptUsername() and this.promptPassword() because the compiler wouldn't let me use login.promptUsername() and login.promptPassword()


In this case you don't want to use the login object anyway. The methods you want to call belong to the Login instance, which this is a reference to, so it is easier/better to use that.

The same rules apply to the ui and loader variables. The belong to the scope of the method in which they were created. You will either need to widen the scope (by using them members of the class) or pass them into the methods that need them:



- Or - the second option, create local instances and pass them on to the methods that need them, which can be safer for some applications:


 
Eric Larsen
Ranch Hand
Posts: 35
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wow, thanks Steve! That was extremely helpful.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!