markzzz
markzzz

Reputation: 47945

Why I cannot instantiate this variable?

I have this piece of code on a JSP file :

// MainHomePage pageApp = new MainHomePage(session);

String pageValue=request.getParameter("page");
if((pageValue!=null) && (pageValue.compareTo("1")==0)) {
    MainHomePage pageApp = new MainHomePage(session);
} else if((pageValue!=null) && (pageValue.compareTo("2")==0)) {
    MainAffittaAppartamenti pageApp = new MainAffittaAppartamenti(session);
} else {
    MainHomePage pageApp = new MainHomePage(session);
}

pageApp.someMethod();

if i don't remove comment on the first line it said (about pageApp) "cannot find symbol"... why this? if-else will instantiate it, in any case. What am I wrong? Cheers

Upvotes: 0

Views: 628

Answers (3)

Kevin Brock
Kevin Brock

Reputation: 8944

Actually, what you have should not compile since pageApp is not within scope on the last line. Looks like you are confused between variable declaration and variable assignment. Java allows you to do both in the same statement, however, you may only declare a variable one time within the same scope.

If you uncomment the first declaration but still leave the others inside the if and else statements, then you will get compile time errors related to duplicate declaration. Within the if and else statement blocks you are declaring this same variable with different types. You should find, if possible, an abstract type for that variable that is shared by the two types you are instantiating (e.g. MainHomePage and MainAffittaAppartamenti) and then declare it outside the if and else blocks and in the main method scope.

For example, if MainAffittaAppartamenti is a sub-class to MainHomePage then you can do this:

MainHomePage pageApp; // Declare here but do not assign

String pageValue=request.getParameter("page");
if((pageValue!=null) && (pageValue.compareTo("1")==0)) {
    pageApp = new MainHomePage(session);
} else if((pageValue!=null) && (pageValue.compareTo("2")==0)) {
    pageApp = new MainAffittaAppartamenti(session);
} else {
    pageApp = new MainHomePage(session);
}

pageApp.someMethod();

Notice that in the inner blocks I did not declare the variable again (that is define it with it's type). Since, given my assumption statement, MainAffittaAppartamenti is a sub-class of MainHomePage then the assignment of that to pageApp is valid still.

Upvotes: 1

AlexR
AlexR

Reputation: 115328

so, you should modify your code as following:

MainHomePage pageApp;

String pageValue=request.getParameter("page"); if((pageValue!=null) && (pageValue.compareTo("1")==0)) { pageApp = new MainHomePage(session); } else if((pageValue!=null) && (pageValue.compareTo("2")==0)) { pageApp = new MainAffittaAppartamenti(session); } else { pageApp = new MainHomePage(session); } pageApp.someMethod();

Now you work with the same pageApp that you defined in the beginning. In your version of code you created local variable that lives in block between if and else only.

Upvotes: 1

Jigar Joshi
Jigar Joshi

Reputation: 240870

If you comment

MainHomePage pageApp = new MainHomePage(session);  

There will not be any variable named pageApp available for pageApp.someMethod();

So compiler says ,

The pageApp you are creating in if else are local to block

About scope of variable

Upvotes: 3

Related Questions