The Artima Developer Community
Sponsored Link

Java Answers Forum
Why it behaves differently?

1 reply on 1 page. Most recent reply: Jan 30, 2004 1:06 AM by Yahari

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 1 reply on 1 page
Yahari

Posts: 2
Nickname: yahari
Registered: Jan, 2004

Why it behaves differently? Posted: Jan 30, 2004 12:34 AM
Reply to this message Reply
Advertisement
After reading Chapter 11 of Thinking in Java,which in about java i/o,I discovered a small problem about the input and output of static members.

Take the following code for example:

import java.io.*;
import java.util.*;

class One implements Serializable{
static int i;
int j;
One(int j){
this.j=j;
i=3;
}
public String toString(){
return "static: "+i+" non-static: "+j;
}
}


class Two implements Serializable{
static int i=4;
int j;
Two(int j){
this.j=j;
}
public String toString(){
return "static: "+i+" non-static "+j;
}
}


class Alles {
public static void main(String[]args)throws Exception{
int i=1;//make it 0 to run part 1,or 1 to run part 2.
//part 1***************************************************************
if(i==0){//what if comment out this.
One o=new One(5);
Two t=new Two(6);
System.out.println(o);
System.out.println(t);
One.i=333;
Two.i=888;
System.out.println(o);
System.out.println(t);

ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("e:/exp/ooo.txt"));
oos.writeObject(o);
oos.writeObject(t);
oos.close();
}else{//and this line
//part 2************************************************************************
ObjectInputStream ois=new ObjectInputStream(new FileInputStream("e:/exp/ooo.txt"));
One o1=(One)ois.readObject();
Two t1=(Two)ois.readObject();

System.out.println("*******************");
System.out.println(o1);
System.out.println(t1);
ois.close();
}//this line.
}
}

As described in Thinking In Java,when part 2 was run,we didn't get the static value which has been changed,the object o1 and t1's static value returned their original value,thus,the static value didn't successfully restored.But what if Commenting out the if-else circle,and joining part1 and part2 together into one?(like this:)

class Alles {
public static void main(String[]args)throws Exception{
int i=1;//make it 0 to run part 1,or 1 to run part 2.
//part 1***************************************************************

One o=new One(5);
Two t=new Two(6);
System.out.println(o);
System.out.println(t);
One.i=333;
Two.i=888;
System.out.println(o);
System.out.println(t);

ObjectOutputStream oos=new ObjectOutputStream(new FileOutputStream("e:/exp/ooo.txt"));
oos.writeObject(o);
oos.writeObject(t);
oos.close();

ObjectInputStream ois=new ObjectInputStream(new FileInputStream("e:/exp/ooo.txt"));
One o1=(One)ois.readObject();
Two t1=(Two)ois.readObject();

System.out.println("*******************");
System.out.println(o1);
System.out.println(t1);
ois.close();

}
}

the result is ,the static value successfully restored,althought I don't think there 're any connection between part1 and part2,nor the file "e:/exp/ooo.txt" makes any changes,which implies that whether with or without the if-else circle,the information on file "e:/exp/ooo.txt" are all the same.Can you tell me the reason why?


Yahari

Posts: 2
Nickname: yahari
Registered: Jan, 2004

Re: Why it behaves differently? Posted: Jan 30, 2004 1:06 AM
Reply to this message Reply
for convenience's sake,I give the result of the code as follows,

firstly,set i=0,compile,and part1 run,the result looks like this:

static: 3 non-static: 5

static: 4 non-static 6

static: 333 non-static: 5

static: 888 non-static 6


then,set i=1,compile,and part2 run,the result:

*******************

static: 0 non-static: 5

static: 4 non-static 6



secondly,comment out the if-else circle,join part1 and part2 into one,and run it,the result is like this:

static: 3 non-static: 5

static: 4 non-static 6

static: 333 non-static: 5

static: 888 non-static 6

*******************

static: 333 non-static: 5

static: 888 non-static 6


as you see,unlike running it twice,if running part1 and part 2 at one time,the static values preserved successfully.

Flat View: This topic has 1 reply on 1 page
Topic: WebappClassLoader not unloaded after webapp reload (Tomcat 4.1.24) Previous Topic   Next Topic Topic: Where do we save the *.JSP files in TomcatServer

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use