Will static variable be serialised
When I serialise a object, will the static veriables also be serialised ?
Static variable's will not be serialized because
they are belongs to Class level and not to any Object in
that class.You need to amend your sentence a bit.
And thanks for posting that informative reply. Where were you all this time?
Similar Messages
-
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 -
Hi All,
How does WebLogic cluster an object, EJB or servlet, with
static variables? I mean, will the static variables in multiple
JVMs still have a single value? Or it just can't be clustered?
Lynch
Statics are only statics per JVM and classloader. This applies to non
clustering environments as well. If you want a singleton you have to use
another approach.
Tinou Bao
www.tinou.com
"Lynch" <[email protected]> wrote in message
news:3b170adb$[email protected]..
> Hi All,
>
> How does WebLogic cluster an object, EJB or servlet, with
> static variables? I mean, will the static variables in multiple
> JVMs still have a single value? Or it just can't be clustered?
>
> Lynch
>
>
-
Static Variable in webapplication
Are the values of static variables preserved when two different users try and access them? i.e.
If one web application user sitting in Chicago sets a static variable to 'true' from 'false'. Will the other user sitting in Omaha see it as 'true' or 'false' in his session?Alright, I did this test.
1) On my local tomcat I created a mini webapp where I
declared a static which populates from a form field.
static String displayname = "";
displayname = request.getParameter("username");
and then displayed it on the html page.
2) I opened IE sessions of my application and in one
I gave the user name 'Tom' and other 'Janet' and hit
continue. (Tom before Janet)
3) The page that had 'Tom' displayed tom and other
one Janet. Then I refreshed the display page for the
tom instance and it still showed 'Tom'.
Conclusion: It appears that the statics are indeed
loyal to the class loader and not the JVM. Hence, for
multiple users each instance should have its own
static.
Please let me know if you disagree or see my test to
be wrong.
Thanks
Message was edited by:
Birthdayhitting the refesh button on your IE page doesn't show you what the value of the static variable in your web app is. You're headding for trouble.
EDIT: Ok well it depends on you implementation how the refresh will work, but if you have a static fields in a class on your web server I promise you it's not in any way associated with any session. -
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 -
Non-static variable cant accessed from the static context..your suggestion
Once again stuck in my own thinking, As per my knowledge there is a general error in java.
i.e. 'Non-static variable cant accessed from static context....'
Now the thing is that, When we are declaring any variables(non-static) and trying to access it within the same method, Its working perfectly fine.
i.e.
public class trial{
���������� public static void main(String ar[]){ ////static context
������������ int counter=0; ///Non static variable
������������ for(;counter<10;) {
�������������� counter++;
�������������� System.out.println("Value of counter = " + counter) ; ///working fine
�������������� }
���������� }
Now the question is that if we are trying to declare a variable out-side the method (Non-static) , Then we defenately face the error' Non-static varialble can't accessed from the static context', BUT here within the static context we declared the non-static variable and accessed it perfectly.
Please give your valuable suggestions.
Thanks,
JeffOnce again stuck in my own thinking, As per my
knowledge there is a general error in java.
i.e. 'Non-static variable cant accessed from static
context....'
Now the thing is that, When we are declaring any
variables(non-static) and trying to access it within
the same method, Its working perfectly fine.
i.e.
public class trial{
���������� public static void
main(String ar[]){ ////static context
������������ int counter=0; ///Non
static variable
������������ for(;counter<10;) {
�������������� counter++;
��������������
System.out.println("Value
of counter = " + counter) ; ///working fine
�������������� }
���������� }
w the question is that if we are trying to declare a
variable out-side the method (Non-static) , Then we
defenately face the error' Non-static varialble can't
accessed from the static context', BUT here within
the static context we declared the non-static
variable and accessed it perfectly.
Please give your valuable suggestions.
Thanks,
JeffHi,
You are declaring a variable inside a static method,
that means you are opening a static scope... i.e. static block internally...
whatever the variable you declare inside a static block... will be static by default, even if you didn't add static while declaring...
But if you put ... it will be considered as redundant by compiler.
More over, static context does not get "this" pointer...
that's the reason we refer to any non-static variables declared outside of any methods... by creating an object... this gives "this" pointer to static method controller. -
Non-static variable aceYears cannot be referenced from a static context
This is my error...
investment.java:53: non-static variable aceYears cannot be referenced from a static context
lwInvest.numberAceYears(aceYears);
^
This is my code...
public static void main (String[] args)
investment.Invest.numberAceYears(aceYears);
and more code
String termInvested = JOptionPane.showInputDialog
("Please enter the amount of years to invest your investment.");
int intTermInvested = Integer.parseInt(termInvested);
and yet more code..
public void numberAceYears (int aceYears)
for (int aY = 5 ; aY <= aceYears; aY++)
double aceInterest = aceCoBalance * aceCoRate /100;
aceCoBalance = aceCoBalance + aceInterest;
aceYears = aceYears + aY;
Suggestions?Short version: Either make the variable in question static, or create an instance of your class and use that to access it. Either one will work, but one probably suits your design better. It's up to you to figure out which one.
For details, see the relevant section of your favorite Java book or tutorial, or poke around here:
http://www.google.com/search?q=java+non+static+variable+cannot+be+referenced+from+a+static+context -
Java static variables in Stored Java
I'm wondering if someone in Oracle could relate their thinking with regards to static variables in stored java and why were these implemented as they were. The aspect I'm referring to specifically is how these are basically working as PLSQL package variables, i.e. with state maintained for the life of the session.
Outside the database, in the "normal" Java world, statics will only live as long as the program is running. But in the Oracle DB world, the static continues to live as long as the session. With this in mind, doesn't this difference compromise the idea of just inserting working Java code into the Database? i.e. static variables now have to be initialized every time a method is called, and cannot be assumed to have default values. Does that not seem like a rather large departure from what a non-DB java programmer would be doing?
Thanks in advance for your thoughts on this.
Joe TsengIt should generally be the case that all calls in a session see the same static variable values. Some actions will terminate the java session (and lose the static variable state) without ending the RDBMS session, such as calling System.exit from java, but normally this can't happen within a single RDBMS call. Perhaps if you give more details on how you are making the call from java to PLSQL one could say more. In particular, if the call happens to be via the JDBC thin driver then the session running in the JDBC connection will be distinct from the calling session.
-
Static variable, a bug in applet classloader, Java plug-in?
I found a potential bug in java plug-in.
Environment,
1. IE 6.0
2. Windows XP
3. JRE 1.3.1.06 and JRE 1.4.1.01
Steps,
1. build a very simple applet (attached below)
2. embed the applet into a .html page.
3. enable java plug-in for IE and Netscape.
4. launch the .html page in either of the two browsers.
5. for the 1st time, I can see 'tmp = 0' in the java console window.
6. keep the browser open, browse to 'www.google.com'
7. click 'Back' button of the browser.
8. I notice 'tmp = 1' in the java console window.
9. if I browse to google.com and back again, it will display 'tmp = 2'. and the number will keep on increasing if I repeat those steps.
10. But if I press F5 to refresh the page, the log message will go back to 'tmp = 0'. and if I press 'x' inthe console window to clean up the classloader cache, I will get 'tmp = 0'. If I close the browser and open a new one, I will still get 'tmp = 0', which is what the applet should output.
11. If I click 't' in the console window to list the thread while I am in the 'google' page, I can't see the applet thread, which means the applet should have been destroyed.
12. No matter I try 'g' to do garbage collect, or 'f' to finalize objects, the problem still happens. the only solution is what I did in the step 10.
Is it a bug in the classloader or the JRE itself? Why the value of the static variable won't be cleaned up along with the applet?
----------- HERE IS THE APPLET --------------------
import java.applet.Applet;
public class AA extends Applet
private static long tmp = 0;
public void init() {
System.out.println("tmp value = " + tmp);
tmp++;
public void start() {}
public void stop() {}
public void destroy() {}Static values are stored in the class. Once a class is loaded into a classloader, it cannot be unloaded (the only thing you can do is use another classloader).
Apparently, the classloader cache allows sharing of classloaders between applet instances. Perhaps F5 (reload) causes the classloader cache to be cleared.
I don't see any bug. When developing applications or applets that must run in the same JVM, you should program defensively and avoid using static values which can change over time. -
IS there static variables in oracle 9i??
Hi,
I have a requirement.
I am establishing an orcale 9i connection from my .net application.
Now I am calling an SP and it has a global count variable initialized to 0, and It fetches a count of * from one table and also there are other functionality that the SP does.
Now I want to persisit this count variable's value , so that next time the SP is called ( in the same connection), I will make a check that if the count variable is not 0 then dont execute the query to populate the count variable.
Normally in java, .net etc... we can create static variables for this purpose.
Can the same be done in PL/SQL?
If not what would be the alternative?
Thanks folks.
sHi,
Please have a look.
SQL> create or replace package test_pkg
2 is
3 procedure test_1;
4 end test_pkg;
5 /
Package created.
SQL> create or replace package body test_pkg
2 as
3 global_cnt number(10):=0;
4 procedure test_1
5 is
6 begin
7 if global_cnt > 0
8 then
9 dbms_output.put_line('No RUN');
10 else
11 dbms_output.put_line('RUN');
12 global_cnt := global_cnt + 1;
13 end if;
14 end;
15 end test_pkg;
16 /
Package body created.
SQL> set serveroutput on
SQL> exec test_pkg.test_1
RUN
PL/SQL procedure successfully completed.
SQL> exec test_pkg.test_1
No RUN
PL/SQL procedure successfully completed.
SQL> exec test_pkg.test_1
No RUN
PL/SQL procedure successfully completed.
SQL>
----------------------------------------------------------------------- -
How to use the dynamical or static variable for ESSBASE cube name?
Hi Experts,
When I import ESSBASE Cube into physical layer, the cube name from ESSBASE is created automatically, such as H_Sales.
I want to use the the static or dynamical variable for replacing the external name. So I try to create the static variable in RPD,such as cubeName, and use the following code
'VALUEOF(cubeName)' into the textbox of external name.
However, when I view the report in answer, it will generate the error message: Database VALUEOF(cubeName) does not exist.
Is it possible to implement this functionality?
Thanks..Hi,
use <%=odiRef.getSchemaName("D")%>
D as parameter if it is the Data Schema or W if you need the schema from Work Schema
Your command will be like:
select <%=odiRef.getSchemaName("D")%>.GER_LOT_EXEC_ODI('Fluxo', 1, 'C') FROM DUAL
Works?
Cezar Santos
http://odiexperts.com -
Can I use static variable in EJB?
Many books suggest developer don't use static variable in EJB,I want to know why?
I know there isn't any problem if the static varibale is read only
For writable static varible ,what will happen if I use a static Hashtable for share data
Help me!Thank you very much!!Greetings,
I know that "EJB business methods are not allowed to
block on synchronized resources" Just where do you "know" that from?? The EJB 2.0 Specification, at least, is nowhere in agrement with this statement. If it comes from a book I would question the author's reasoning. Contractually, there's no sound basis for this. In the case of Session Beans, they have an expressedly direct and single-threaded association with a client. From a design viewpoint, it certainly seems unnecessary for a bean to "block" its one-and-only client, but to say that it "is not allowed to" do so is without merit. Entity Beans, on the other hand, are concurrently accessible. Yet, with regard to a transactional context being in effect, the container does indeed block on a bean's business methods. For the bean to do so itself is, therefore, unnecessary. Furthermore, the specification explicitly concedes that a "Bean Provider is typically an application domain expert" and "is not required to be an expert at system-level programming." (EJB 2.0 Spec. 3.1.1) From these statements alone it is reasonable to assume the above statement is meritless since the Bean Provider is not expected to even consider synchronization issues when developing a bean.
But I'm mixed up why we could use "Hashtable" or otherApparently, because your sources are as well...
collection classes in the EJB ,in these method many
methods are synchronized In fact, not only "can we use" them but, with respect to multiple-row finders in Entity Beans we are [i]required to use them (or an iteration of them)! Not all Collection classes are synchronized (so called "heavy-weight collections"). As shown above, that the choice of a particular Collection class might be synchronized is of little consequence since a bean designed under strict adherence to the specification ensures that it is never concurrently writeable.
Could someone provide a good way for this problem?
Please Help Me!!!Regards,
Tony "Vee Schade" Cook -
Dealing with a turn based application with using a global static variable
Hi there. I read some articles about JavaFX concurrency,so as a newbie i have to practice on it. Now i'm trying to implement a turn based application that can be played among 3-6 players. Moreover, i use a scene imported from a .fxml file and i have to update it correctly depends on some calculations of each thread (in other word players ). My main issue is, i don't wanna use a while loop which checks the state of game like;
while( GameState != State.GAME_OVER ){
currentPlayer = GameBoard.getNextPlayer();
// do some actions,calculations etc.
So, i want to use worker threads instead of this while loop. I guess using Service class for iterating and assigning the next player will be suitable instead of using the while loop and Tasks for doing each player's calculation,or waiting for some responses from Human players on UI based,however, i'm struggling with two problems.
There should a global static variable ( like GameState which is an Enumarator ) determines the state of the game,so it should be updated and should be checked by each turn. Is there any way to do this ?
How can i get rid off this while loop ?
I will appreciate for every answer. Thanks anyway.It shouldn't make too much difference. The basic idea is that you have a model class representing your game state (the Game class in jsmith's example). When a player makes a move, you update the state of the game. Since this will result in changes to the UI, this update should be performed on the FX Application Thread.
If the player making the move is a human player, the move will likely be made by a user action (button press or mouse click, etc); this will be handled on the FX Application thread anyway.
When the state of the game changes so it is the turn of an "artificial" player to make a move, have the object representing the artificial player calculate its next move, and then update the game state. Since this is a response to the game state changing (it's now the turn of the artificial player), this will also be on the FX Application Thread.
The only (slight) complexity comes if the calculation of the move for the artificial player takes a long time. You don't want to perform this long running calculation on the FX Application Thread. The cleanest way to manage this is to launch a Task which calculates the desired move, and then updates the game state when the move is ready. So, something like this:
GameState game = ... ;
// UI is bound to the game state.
ExecutorService executorService = ... ;
final Player currentPlayer = game.getCurrentPlayer() ;
final Task<Move> calculateMoveTask = new Task<Move>() {
@Override
public Move call() {
Move move = // compute next move...
return move ;
calculateMoveTask.setOnSucceeded(new EventHandler<WorkerStateEvent>() {
@Override
public void handle(WorkerStateEvent event) {
gameState.makeMove(currentPlayer, calculateMoveTask.getValue());
executorService.submit(calculateMoveTask);
If you're doing more threading than that, you're probably doing it wrong... . There should also be no need for anything to be held as a "global" static variable (the idea above is the only structural modification you need to the example posted by jsmith). -
Static variable behaviour in memory
Hi,
My question is :
When you compile a java code, does the static variable gets the memory allocated while creating the byte code or is it on run time that a static variable gets the memory allocation and then it is shared for all instances.
Thanks
Melvyn.Good !!! I expected this question from you.
Instantiation and Initialization are different.
A class or interface type T will be initialized immediately before the first occurrence of any one of the following:
1) T is a class and an instance of T is created.
2) T is a class and a static method declared by T is invoked.
3) A static field declared by T is assigned.
4) A static field declared by T is used and the reference to the
field is not a compile-time constant. References to
compile-time constants must be resolved at compile time to
a copy of the compile-time constant value, so uses of such a
field never cause initialization.
Regards
Raghu -
I want to use static variable instead of using variable in servlet context
Hi all,
In my web application i have to generate a unique Id.
For this, At the application startup time i am connecting to the database and getting the Id and placing it in the servlet context.
Every time i am incrementing this id to generate a unique id.
But, now i want to place this id in a static variable which is available to all the classes.
why i want to do this is to reduce burden on servlet context.
my questing is, is this a best practice ? If not please give me your valuable suggestion.
thanks
tiruThere isn't a problem with this as long as you want to share the value of that variable with all requests. If this is read-only except when it is first set then you're fine. The only real issue will be how to initialize and/or reinitialize the variable. When the servlet is started, how will you get the value for the variable? If the servlet is shutdown and restarted (a possibility in any application server) how will you re-read the variable? You need to answer these questions to decide the best route. It may be as simple as a static initializer or it may be more complex like a synchronized method that is called to see if the variable is set.
Maybe you are looking for
-
How To "Start From Scratch" and Build a Clean iTunes Library on a Portable USB External Drive?
A while ago I posted about my confused (and confusing) mess of an iTunes: https://discussions.apple.com/message/16183210#16183210 While dozens looked I guess eveyone agreed with me "what a mess" and moved on. So ... someone at the Apple Store suggest
-
Hi, I created a PO in our production system. Following are the condition types which gets picked based on the process 1. When i create a material and a inforecord in the PO the condition type which is used in J3AP 2. When i create a material and not
-
Hello: How do I unlock my iPod Classic?
-
Question de Import..
Hi all, i have created native directory and application files using LCM in one environment. I wanna import the whole application into another environment. My what i wanna do is i want to use a different user, a different database, a different applica
-
I tried downloading the latest flash player. Three times it stopped at 47 percent and told me to close internet explorer. I went for help and checked my internet tools, tried everything suggested. I clicked retry and the only option left was to abort