JWorld@TW the best professional Java site in Taiwan
      註冊 | 登入 | 全文檢索 | 排行榜  

» JWorld@TW » Java 新手區  

按列印兼容模式列印這個話題 列印話題    把這個話題寄給朋友 寄給朋友    訂閱主題
reply to topicthreaded modego to previous topicgo to next topic
本主題所含的標籤
無標籤
作者 何時才須要declare static variables?
jimmyisking





發文: 22
積分: 0
於 2007-04-07 07:04 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
public class SeasonWinner
{
    private Schedule schedules;
    public static void main (String [] args)
    {
        SeasonWinner runn = new SeasonWinner ();
    }
public SeasonWinner ()
    {
        run ();
    }
public void run(){
  schedules = new Schedule ();
            System.out.println (Teams [i] + "  -  " + schedules.winRecord [i] + " wins,   " + schedules.looseRecord [i] + " losses,  " + schedules.Tie [i] + " ties =  " + sum [i]);
}
}
class Schedule 
{ 
public static final int GAMES = 20; 
public static String [] teamHome = new String [GAMES]; 
public static String [] teamAway = new String [GAMES]; 
public static int [] winRecord = new int [6]; // Records為什麼不是static 就不會計 
public static int [] looseRecord = new int [6];//為什麼不是static 就不會計 
public static int [] Tie = new int [6];//為什麼不是static 就不會計 
 
public String [] getTeams () 
{ 
String [] Teams = {"Calgary", "Vancouver", "Edmonton", "Toronto", "Ottawa", "Montreal"}; 
return Teams; 
} 
public Game getGame (int i) 
{ 
return new Game ("", "", 0, i); 
} 
} 
 
class Game 
{ 
private Schedule teams; 
public Game (String homeTeam, String awayTeam, int homeScore, int awayScore) 
{ 
} 
public void record () 
{ 
teams = new Schedule (); 
String [] match = teams.getTeams (); 
 
for (int i = 0 ; i < 6 ; i++) 
{ 
if (winner.equalsIgnoreCase (match [i])) 
{ 
teams.winRecord [i] += 1; 
} 
} 
} 

========================================
How come all the winning records of every team become "0" when I change public static int [] winRecord ==> public int [] winRecord.
何時才須要declare static variables?為什麼不是static 就不會計 winning record.
Thank you all~ Smile


jimmyisking edited on 2007-04-10 08:49
reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:jimmyisking]
kid1945





發文: 22
積分: 0
於 2007-04-10 05:32 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
you forgot to create Schedule instance before use
your program workflow is
SeasonWinner runn = new SeasonWinner ();
run ();
System.out.println (Teams [i] + " - " + schedules.winRecord [i]....

the instance of schedule not yet created


reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:jimmyisking]
kid1945





發文: 22
積分: 0
於 2007-04-10 06:00 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
i suggest you learn more before practically write a program
the code is just too crazy and difficult to read


reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:kid1945]
jimmyisking





發文: 22
積分: 0
於 2007-04-10 06:06 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
kid1945 wrote:
i suggest you learn more before practically write a program
the code is just too crazy and difficult to read

I didn't post all my code~ and that is not the problem i am asking~
My program work perfectly on its own .
REad my question before you reply ~ Question


reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:jimmyisking]
kid1945





發文: 22
積分: 0
於 2007-04-10 07:58 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
why teams.winRecord += 1 but printout schedules.winRecord as result
if you want to use instance variable

have you create schedules instance ? if so please paste it, so that we can help
we don't know what happened between teams and schedules

if you have not create it
you still can access the static variable winRecord via runn.schedules.winRecord from the instance method (why don't SeasonWinner.winRecord if you want static?)
that why the JVM won't alarm you (eclipse will just warn)
but doesn't mean this is a good programming convention


kid1945 edited on 2007-04-10 08:43
reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:kid1945]
jimmyisking





發文: 22
積分: 0
於 2007-04-10 08:43 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
kid1945 wrote:
why teams.winRecord += 1 but printout schedules.winRecord as result
if you want to use instance variable

In case you didn't see ~Sad
=============
teams = new Schedule ();
=============
"Therefore"
teams.winRecord ~
&
1
2
3
4
5
6
7
8
public class SeasonWinner{
private Schedule schedules;
public void run(){
  schedules = new Schedule ();
            System.out.println (Teams [i] + "  -  " + schedules.winRecord [i] + " wins,   " + schedules.looseRecord [i] + " losses,  " + schedules.Tie [i] + " ties =  " + sum [i]);
}
}
 

=============
Get it now ~ Question
Once again, please do read things carefully ~ Sad


jimmyisking edited on 2007-04-10 08:54
reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:jimmyisking]
kid1945





發文: 22
積分: 0
於 2007-04-10 08:53 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
hey....
teams = new Schedule (); // you create Schedule and assign reference into teams #say x001

public class SeasonWinner{
public static void main(String[]args){
printout schedules.winRecord
}

schedules.winRecord // what dose schedules store? must not x001
you don't create or assign object reference into schedules - this instance variable
(instance variable store object reference)

schedules stored null since you just Schedules schedules; in which without any initialization or assignment
so just leave it null
and you run it from instance runn's method and it call the static variable
so the code just work fine only if you define the data as static


reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:jimmyisking]
kid1945





發文: 22
積分: 0
於 2007-04-10 09:08 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
sorry just miss it
schedules = new Schedule (); <- found it

there are 2 schedule instance
one store into teams
and one just create by run

teams store the true result
but
schedules just store 0


reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:kid1945]
jimmyisking





發文: 22
積分: 0
於 2007-04-10 09:21 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
kid1945 wrote:
sorry just miss it
schedules = new Schedule (); <- found it

there are 2 schedule instance
one store into teams
and one just create by run

teams store the true result
but
schedules just store 0

yeah~ that should be the case.
From what I was taught, Schedule team = new Schedule ();
should create a copy of all the variables in that class.(u can see that in an object diagram)
so teams' copy of Schedule store all the variable.
==============
However, I still don't get what static really do~
I know static should be assigned to variables that doesn't change often
Ex: Math.pi is static becuase its always 3.14..
so why does my static variable make such a difference here ~?
does static means pass by reference instead of copy? that is why only static help me to read the last updated version of those records?


jimmyisking edited on 2007-04-10 09:25
reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:jimmyisking]
kid1945





發文: 22
積分: 0
於 2007-04-10 09:29 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
static var is global var

instance var you must create an instance before access the var
and each var follow by each instance

static var you can use it without create an object
just use classname.varname to use it
and static only ONE copy they use same var

i scratch the code for you
try pass schedule rather than create another one

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
public class Test3 {
  private Scheduler scheduler;
 
  Test3() {
    run();
  }
 
  public static void main(String[] args) {
    Test3 test3 = new Test3();
 
  }
 
  public void run() {
    scheduler = new Scheduler();
    System.out.println(scheduler.score);
  }
}
 
class Scheduler {
  int score = 0;
  private Game game;
 
  Scheduler()
  {
    game = new Game(this);
  }
  
  public Game getGame(){
    return game;
  }
}
 
class Game {
  Scheduler sc;
  Game(Scheduler sc){
    this.sc = sc;
    record();
  }
  public void record() {
    //dummy record
      sc.score=20;
  }
}


kid1945 edited on 2007-04-10 09:36
reply to postreply to post
作者 Re:何時才須要declare static variables? [Re:jimmyisking]
jimmyisking





發文: 22
積分: 0
於 2007-04-10 09:36 user profilesend a private message to userreply to postreply to postsearch all posts byselect and copy to clipboard. 
ie only, sorry for netscape users:-)add this post to my favorite list
yeah ~ I get it now
i can just use Schedule.winrecord[i];
===
http://www.caterpillar.onlyfun.net/Gossip/JavaGossip-V1/AboutStaticMember.htm
==
Thanks alot ~ now i understand when to use static variables ~


reply to postreply to post
» JWorld@TW »  Java 新手區

reply to topicthreaded modego to previous topicgo to next topic
  已讀文章
  新的文章
  被刪除的文章
Jump to the top of page

JWorld@TW 本站商標資訊

Powered by Powerful JuteForum® Version Jute 1.5.8