Declaring static variable in JSP
<%
static Logger browserDetectLog = Logger.getLogger(ScoConstants.LOGGER_BROWSERDETECT);
%>
I m declaring the logger variable like dis.
It gives me error, whereas in java class it does'nt.
Please tell me the difference.
Anything in <% %> delimiters is put inline.
Anything in <%= %> is put inline but without closing the current out.print().
Anything in <%! %> is put outside of the _jspservice() method.You should be doing
<%!
static Logger browserDetectLog = Logger.getLogger(ScoConstants.LOGGER_BROWSERDETECT);
%>
Similar Messages
-
Is it a bad way to declare somany Static Variables in a program?
-Achyuth Bok..
So if in a swings program if i am using only one frame through out the program, by adding and deleting components on it.
Then i can declare that frame as Static, right.
So that i can call the same frame(and not instance of it) from different class.
And if i have to take the value entered in a JTextField from another class, Then that JTextField can also be declared as Static right.
-Achyuth B -
Hi All,
I have written the following code, but its not compiling saying that "Class or interface declaration expected. static "
I dotn know whats wrong in this scriplet though.. any help is greatly appretiated.
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=ISO-8859-1">
<title>JSP Scriplet</title>
</head>
<body>
<%
static int counter = 15;
for(int i = 0; i < 15; i++){
int p = returnCounter();
if(p == 0)
System.out.println("I am the 15th user");
public static int returnCounter(){
counter = counter - 1;
System.out.println("counter : " + counter);
return counter;
%>
</body>
</html>Simply put, you're Java code is wrong.
The detail is that essentially all of the code in your JSP file is in one big method.
So, here's what your code looks like to the compiler:
public void _internalJspDoPageCode() {
static int counter = 15;
public static int returnCounter() {
}You can't simply nest methods like this in Java.
If you want to, you can try this:
<%
public class mycounter {
static int counter = 15;
public static int returnCounter() {
counter = counter - 1;
return counter;
for (int i = 0; i < 15; i++) {
int p = mycounter.returnCounter();
if(p == 0)
System.out.println("I am the 15th user");
%>That will create an inner class with a static member, which is basically what you're trying to do.
But note that this inner class is only accessible from this JSP page, and no where else. -
Declaring static variable to save memory, memory cost of link
If I have plenty of objects of the same class and they have a field that is the same for all objects, do I save memory by declaring it 'static'?
How much memory is used by an object to store a link that points to another object?
/CarlisOk!
I am making a map for a computer game, and it has plenty of Towns. Now, every object of type 'Town' has a list with different Tribes
String[] Tribes= {"Roman", "Teuton", "Gaul"};
This list is identical for all objects of type 'Town'.
Now, if I declare it to be static like this;
static String[] Tribes={...};
Does this save memory? If I understand this correctly, the 'static' keyword means that the 'String[] Tribes' is only stored once rather than in every object of type 'Town'. -
Can't we declare a static variable inside a memberfunction of a class?
Hi,
class A{
public void fun()
static int i=10;
can' we declare static variable in member function of class?
Thanks,It is a common idiom in C and C++, but it is forbidden
in Java because it adds hidden dependencies.
The C way of writing a serial number generator:
int generate() {
static int n = 0;
return n++;
}Pure C has only global functions. So it needs inner
static variables to help to hide the data. I've had
lots of headaches trying to make C programs with inner
static variables work correctly because they usually
are hidden in cross-reference listings.
The Java way:
public static class SerialNumberGenerator() {
private static int n = 0;
public static int generate() {
return n++;
}The code above is as static as the C code given
before, but it tries to be more explicit (no hidden
variables).Hum... have you tried to compile your sample ?
(And anyway, what the hell would a static class be used for ???)
But perhaps you meant:
public final class SerialNumberGenerator {
private static int n = 0;
public static int generate() {
return n++; -
Static variable in session bean
Can we declare static variable in session bean. If we declare what will happen. Will it create error in compile time or not deployed in server.
From a Java language perspective, nothing stops you from declaring a static variable in a session
bean class. It will compile as long as its syntactically correct.
From an EJB programming model perspective, the use of non-final static variables
is discouraged because it breaks the JVM-transparency that is an important aspect of the
EJB architecture. It should be possible to deploy a single EJB application to a cluster and
have it behave exactly as if it were deployed to only one server instance (albeit with higher
overall throughput/performance). Using non-final static variables breaks this property
because the bean instances in one JVM will see a different value for the static variable
than bean instances in a different JVM.
It also forces you to deal with synchronization
of the shared data, which is a complexity that was carefully avoided in the EJB programming
model by ensuring that each bean instance is single-threaded.
Bottom line is you can have "final static" data members in EJB classes but you should
avoid non-final (mutable) static data.
--ken -
Using a static variable declared in an applet in another class
Hi guys,
I created an applet and i want to use one of the static variables declared in teh applet class in another class i have. however i get an error when i try to do that...
in my Return2 class i try to call the variable infoPanel (declared as a static JPanel in myApplet...myApplet is set up like so:
public class myApplet extends JApplet implements ActionListener, ListSelectionListener
here are some of the lines causing a problem in the Return2 class:
myApplet.infoPanel.removeAll();
myApplet.infoPanel.add(functionForm2.smgframeold);
myApplet.infoPanel.validate();
myApplet.infoPanel.repaint();
here are some of the errors i get
dummy/Return2.java [211:1] package myApplet does not exist
myApplet.infoPanel.removeAll();
^
dummy/Return2.java [212:1] package myApplet does not exist
myApplet.infoPanel.add(functionForm2.smgframeold);
^
dummy/Return2.java [213:1] package myApplet does not exist
myApplet.infoPanel.validate();
^
dummy/Return2.java [214:1] package myApplet does not exist
myApplet.infoPanel.repaint();
^
please help! thanks :)I don't declare any packages though....i think it just doesn't recognize myApplet for some reason..
other errors i got compiling are:
dummy/Return2.java [82:1] cannot resolve symbol
symbol : variable myApplet
location: class Return2
updateDesc.setString(3, myApplet.staticName);
I Don't get why i'm getting this error cuase they worked fine when myApplet was a standalone application, not an applet.
myApplet is in the same folder as Return2 and it compiles properly. -
Static variable declaration in JavaFX
Hello, I'd like to ask how to substitute static key word to declare a variable, that is visible within the current package and accessible without creating an instance of a class, that contains this variable. Because my NetBeans insists that static variable is depricated.
Thank you.In Foo.fx...
class Foo {
// bar is an instance variable that has public access
public def bar = "bar";
// baz is a "static" variable that has public access
public def baz = "baz";
// someFun is a "static" function
public function someFun() : Void {
}In Main.fx...
println("{Foo.bar}"); // compile error - cannot reference bar from static context
println("{Foo.baz}"); // This is ok - Foo.baz is static
var foo = Foo{}
println("{foo.bar}"); // This is ok. bar referenced from Foo instance -
Clarification about static variables declaration
I'm getting "Illegal Start of Expression" error, When i try to declare a static variable within a static metho or nonstatic method.
Could you please clarify me,
Is it possible to declare a static varible within a static method or non static method ?
ThanksHi Vikas,
First, note that this forum is devoted to Sun Java Studio Creator IDE. General Java questions can be asked on forums here: http://forum.java.sun.com/
Second, note that static variable can be defined only as member of class. It cannot be defined inside of method.
Thanks, Misha
(Creator team) -
How to use an BPM Instance Variable in JSP page
Hi All,
I am using the JSP Presentation, but i don't know how to use an Instance variable in JSP page, that instance already declared in the process. And Can u explain the syntax that to include the JS file into jsp page
Regards
Vasu.
Edited by bpmvasu at 04/03/2007 10:43 PMHi Mariano,
I'm using JSP presentation too. In "Interactive Component Call" active i'm using "Use JSP presentation", but i only can define one instance variable, i need to add more instance variables. In "Advanced" option of this task, i have the argument mapping .. but i don't understand how to use it.
I have a instance variable called "genders" of the type String[Int] (Associative Array) and i'm mapping this instance variable in "Arguments Show In" option of the advanced option of JSP presentation. In JSP presentation i have the code:
<select <f:fieldName att="person.gender"/>>
<c:forEach var="gender" begin="0" items="${genders}" varStatus="status">
<c:choose>
<c:when test="${person.gender == gender}">
<option value="<c:out value="${gender}"/>" selected="true"><c:out value="${gender}"/></option>
</c:when>
<c:otherwise>
<option value="<c:out value="${gender}"/>"><c:out value="${gender}"/></option>
</c:otherwise>
</c:choose>
</c:forEach>
</select>And in my screenflow i have the code:
genders[0] = "Male"
genders[1] = "Female"But when i run my application, i have the error: "The task could not be successfully executed. Reason: 'java.lang.ClassCastException: java.lang.Integer'."
What's the problem? -
Declare a variable in an Interface
hi all
can i declare a variable in a interface , something like
public static final String dsn=" // something";
so that i dont have to declare the same thing in all the classes that implement this interface
regardsYou don't need the public, static, or final. All fields in interfaces are implictly public static final fields.
-
Using Static Variable against Context Attribute for Holding IWDView
Dear Friends,
I have a method which is in another DC which has a parameter of the type IWDView. In my view, I will have an action which will call the method in another component by passing the value for the view parameter. Here, I can achieve this in 2 types. One is - I declare a static variable and assign the wdDoModifyView's view as parameter value and I can pass this variable as parameter whenever calling that method or the second way - create an attribute and assign the same wdDoModifyView's view parameter as its value. Whenever I call this method, I can pass this attribute as parameter. What is the difference between these two types of holding the value since I am storing the same value i.e., wdDoModifyView's view parameter. But when I trigger the action from different user sessions, the first type of code (using static variable) prints the same value in both the sessions for view.hashCode() and View.toString(), but the same is printing the different values when I pass the attribute which holds the view parameter.
Clarification on this is highly appreciated
The problem I face is when I use static variable to get the view instance and export the data using the UI element's id, the data belonging to different user sessions is mixed up where as when I use Context Attribute, the same problem doesn't arise. I want to know the reason why it is so. Is there any other place or way where I can get the current view instance of each session instead of wdDoModifyView?Hi Sujai ,
As you have specified the problem that we face when we use static attributes, when end users are using the application .
Static means i have n number of objects but the static variable value will remain same every where.
when it is context attribute for every object i.e nth object you have a nth context attribute i mean nth copy of the context attribute.
so every user has a unique Iview parameter , when context is used and
when static is used , assume you have userA , his iview is set this intially and u have another user B , when he is using , since the variable is static and when you access this variable you will get the value of userA.
Regards
Govardan Raj -
Would like to declare a variable Public/Global in an IF statement
Is there way to declare a variable as Public in an IF statement. My goal is to declare the variable as global (since this variable is used somewhere in the code and should not get initialized when there is loop back to the top of the code, hence using IF statement to control the initialization) based on the IF condition and then use it elsewhere in the code.
I have it like this
if (!sei_second_jsp.equals("1"))
public int[] mecitem;
public String[] sei_mfg_prod_cat;
public String[] sei_part_number;
public String[] sei_descrip;
public String[] sei_price;
public int[] sei_onhand;
public int[] sei_demand;
mecitem = new int[20000];
sei_mfg_prod_cat = new String[20000];
sei_part_number = new String[20000];
sei_descrip = new String[20000];
sei_price = new String[20000];
sei_onhand = new int[20000];
sei_demand = new int[20000];
for (int i=0; i<=19999; i++)
mecitem[i] = 0;
sei_mfg_prod_cat[i] = " ";
sei_part_number[i] = " ";
sei_descrip[i] = " ";
sei_price[i] = " ";
sei_onhand[i] = 0;
sei_demand[i] = 0;
Your guess is right, I am using this code in JSP - since this is a Java related question, thought of posting it in JAVA forum.
When I use the above code, I get the following error
1809 }' expected. {
1811 Statement expected. public int[] mecitem;
1827 Identifier expected. mecitem = new int[20000];
1827 Can't specify array dimension in a declaration. mecitem = new int[20000];
1827 Identifier expected. mecitem = new int[20000];
1837 Can't specify array dimension in a declaration. sei_onhand = new int[20000];
1837 Identifier expected. sei_onhand = new int[20000];
1839 Can't specify array dimension in a declaration. sei_demand = new int[20000];
1839 Identifier expected. sei_demand = new int[20000];
3117 Class or interface declaration expected. }Please note the above code in the JSP is submitting to itself and I donot want it to get initialized if the IF statement is not successful..
thnks a lot for your time.. !!! -
How to give different value to a static variable???
Hi all:
Is there any solution to set different values to a static variable???I had try two ways but all have errors!
1.Way_1
protected String tmp=null;
protected void initSituation(int sayorpress)
if (sayorpress==0)
tmp = "string1";
else if (sayorpress==1)
tmp = "string2";
protected static String RESOURCE_STRING = tmp; <---error
Error:non-static variable tmp cannot be referenced from a static context
2.Way_2
protected void initSituation(int sayorpress)
if (sayorpress==0)
protected static String RESOURCE_STRING = "string1"; <---error
else if (sayorpress==1)
protected static String RESOURCE_STRING = "string2"; <---error
Error:illegal start of expression at
not an expression statement at
Thank you very mich!!!Try this:
protected static String RESOURCE_STRING = null;
protected void initSituation(int sayorpress)
if (sayorpress==0)
yourClass.RESOURCE_STRING = "string1";
else if (sayorpress==1)
yourClass.RESOURCE_STRING = "string2";
You cannot declare a static variable inside a method. But you can access a static variable thorugh your class. -
To use a static variable in another class,but report NullPointerException
when TableMain is running,I run testRecord so that let TableMain add a occur informatin and
happened time in a row in TableMain,but when I run testRecord,java report a NullPointerException and I dont know how to solve this problem,thanks for helping me to check my code;(error report info is in end)
import java.awt.event.*;
import javax.swing.table.*;
import java.io.*;
public class TableMain extends JFrame{
JTable table;
static OwnModel model;
String[] columnHeader={"occur","time"};
class OwnModel extends DefaultTableModel{
public OwnModel(Object[] columnNames,int numRows){
super(columnNames,numRows);
public boolean isCellEditable(int row,int column){
return false;
public TableMain(){
model=new OwnModel(columnHeader,0);
table=new JTable(model);
JScrollPane scroll=new JScrollPane(table);
JButton save=new JButton("save record");
JButton delete=new JButton("delete record");
JPanel buttons=new JPanel();
buttons.add(save);
buttons.add(delete);
JLabel sign=new JLabel("occur record");
Container cp=getContentPane();
cp.add(BorderLayout.NORTH,sign);
cp.add(BorderLayout.CENTER,scroll);
cp.add(BorderLayout.SOUTH,buttons);
this.setSize(300,500);
this.setVisible(true);
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
public static void main(String[] args){
new TableMain();
import java.util.Vector;
import java.util.Calendar;
import java.text.SimpleDateFormat;
public class testRecord{
public static void main(String[] args){
SimpleDateFormat simpledf=new SimpleDateFormat("yyyy/MM/dd hh:mm:ss");
String tableName="friends";
Calendar occurtime=Calendar.getInstance();
Vector record=new Vector();
record.add("send 'desc' sql, "+"operated table is :"+tableName);
record.add(simpledf.format(occurtime.getTime()));
System.out.println("model is "+Guide.model);
System.out.println("record is "+record);
Guide.model.addRow(record);
error report:
model is null
record is [send 'desc' sql,operated table is :friends, 2004/05/26 11:39:03]
Exception in thread "main" java.lang.NullPointerException
at testRecord.main(testRecord.java:14)but I just use this constructor once,never use twice
with same jvm;I thinks my idea is not too very badIt's a public constructor (if I'm looking in the right
place) - it could be called from anywhere. To
intialize a static variable outside the declaration,
use a static initializer:private static String whatever;static {
whatever = "whatever";
}Or simply (in this case):private static String whatever = "whatever";
Maybe you are looking for
-
My Apple ID verification e-mails are not coming through to my e-mail account.
My Apple ID verification e-mails are not coming through to my e-mail account. How can I fix that?
-
Solution for 4g ipods which arent recognized by itunes
go buy a USB 2.0 hub, it costs around $6 here in Manila where im from. i used to use my pod with a firewire cable (the usb 2.0 card thats attached to my pc just wont work in my pod) but now, usb works with this new usb 2.0 HUB!!
-
Uninstall Oracle EPM 11.1.1.3
We are unable to Uninstall Oracle Hyperion 11.1.1.3 installed WIN 2003 X64 bit machine. Throwing JAVA runtime error. Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException at com.hyperion.install.wizard.swing.UninstallProductSelectionF
-
Hi all, There are any Java to exe packager for Pocket PC like on a PC? If so, please refer a link to download or adquire. Thanks Best Regards
-
Is Jdeveloper so full of bugs?
Somebody knows why this application gives the exception below? package pippo; import oracle.dacf.dataset.*; import javax.infobus.RowsetAccess; import javax.infobus.ImmediateAccess; import javax.infobus.DbAccess; public class Application1 { SessionInf