views:

63

answers:

6

the code below is not giving me the answer i want, i don't know where is the problem? FR is the translation of EN (exactly like .properties file) i want to read the translation from the FR.java file if i want to reach the hello variable of fr.java or en.java from the index.jsp page. but code i wrote gives me the value from Lang.java

String language = "FR";

the condition is in the .jsp file jdk 1.4 gives me this error :Error(23,23): variable lang might not have been initialized

any body can help, code pleas?

file name Lang.java

package mypackage;

abstract public class Lang {
  public String hello= "home page";
}

filename EN.java

package mypackage;

public class EN extends Lang {
  public String hello = "hello";
}

filename FR.java

package mypackage;

public class FR extends Lang {
  public String hello = "bonjour";
}

file name : index.jsp

<%@ page contentType="text/html;charset=windows-1252"%>
<%@ page import="mypackage.Lang" %>
<%@ page import="mypackage.FR" %>
<%@ page import="mypackage.EN" %>
<html>
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=windows-1252">

    <title>language</title>
  </head>
  <body>
    <%
      String language = "EN";
      Lang lang;
      if (language.equals("EN")){
        lang = new EN();
        }
      else if (language.equals("FR")){
        lang = new FR();
      }
    %>
    <%
      out.print(lang.hello);
    %>
  </body>
</html>
A: 

If language is not equals to EN or FR then Lang lang will not be initialized. Set it explicitly to new EN() before the if.

rics
+2  A: 
<%
      String language = "EN";
      Lang lang;
      if (language.equals("EN")){
        lang = new EN();
        }
      else if (language.equals("FR")){
        lang = new FR();
      }
    %>  

Here it can be the case where language stays un initialized so you need to initialize it

say

Lang lang = null;//or any default value   

And to initialize local variable is compulsary

I don't understand the importance of this condition here you are assigning "EN" to language then what is the need of condition?

org.life.java
A: 

Set a default value for the lang variable:

Lang lang = new EN();
 if (language.equals("EN")){
     lang = new EN();
 }
 else if (language.equals("FR")){
    lang = new FR();
 }
gedim
+1  A: 

Change the condition as follows:

if (language.equals("FR")){
    lang = new FR();
}
// add more languages here
// example:
// else if (language.equals("DE")) {
//     lang = new DE();
// }
else {
    lang = new EN();
}
Raoul Duke
+1  A: 

Your code does not cater with the case where language is neither "EN" nor "FR". Pick a default language so that the lang variable is always initialised:

Lang lang;
if (language.equals("EN")){
    lang = new EN();
}
else // default case
    lang = new FR();
}

In addition, instead of declaring one attribute for each string that needs to be translated (e.g. String home) consider declaring a method that will take a "key" as a parameter, and return the corresponding translation for that key. For example, in your JSP you could use something like this:

out.print(lang.getString("home"));

Then have all your Lang subclasses return the proper, translated value for each key.

You might also want to have a look at ResourceBundles.

Grodriguez
i want to reach the hello variable of fr.java or en.java from the index.jsp page. but code i wrote gives me the value from Lang.java.
ammar
The code you posted is accessing the `home` variable, not the `hello` variable. The `home` variable only seems to be defined in Lang.java, not in EN.java or FR.java.
Grodriguez
sorry men,it was my mistake in the question, i edited the code. pleas check it again.
ammar
Class variables cannot be overriden in subclasses. You can override class methods, but only "hide" class variables. Instead, consider my suggestion of declaring a `getString` method in class `Lang`, then overriding this method in subclasses to return the proper translation for each key.
Grodriguez
A: 

If you wanted to print hello variable, declare that variable in abstract Lang class and initialize in concrete classes.

package mypackage;

abstract public class Lang {
  public String hello= "home page";
}

filename EN.java

package mypackage;

public class EN extends Lang {
   {
      hello = "hello";
   }
}

filename FR.java

package mypackage;

public class FR extends Lang {
     {
      hello = "bonjour";
   }
}
Jaydeep