Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3406620
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T05:39:01+00:00 2026-05-18T05:39:01+00:00

I have this piece of code on a JSP file : // MainHomePage pageApp

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T05:39:01+00:00Added an answer on May 18, 2026 at 5:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have this piece of code List<Class<? extends SubApplication>> appClasses = new ArrayList<Class<? extends
I have this piece of code, which copies a plist file to the ApplicationSupport
I have this piece of code: <%= link_to New User, new_user_path, :class => button
I have below piece of code in one my JSP <%@ page language=java import
I have this piece of code #include <stdio.h> #include <stdlib.h> #include <stdint.h> #include <string.h>
I have this piece of code: WebClient web = new WebClient(); System.IO.Stream stream =
I have this piece of code ( on jsfiddle ) var paper = new
I have this piece of code: EventSnapshot snapshotEvent = new EventSnapshot(); EventSnapshot currentEvent =
I have this piece of code on a php page, at the bottom: window.opener.location.href=/index.html;
I have this piece of code: listTable.addMouseListener(new MouseAdapter() { public void mouseClicked(MouseEvent e) {

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.