Results 1 to 18 of 18
Like Tree4Likes
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By Fubarable
  • 1 Post By Fubarable

Thread: Why final?

  1. #1
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Why final?

    Java Code:
      public void TimerSet(final Tank t){
            int delay=1000*10;
            ActionListener task=new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
    sm.removeSo(t);
    }
    };
     Timer timer=new  Timer(delay,task);
            timer.start();
    }
    Hi.
    This method is defined in Tank class and is called in Tank class itself.And sm is a class that has removeSo method.removeSo can remove Objects from an arrayList.
    Now my question is that why Tank t(The argument of SetTimer method) should be final?
    Thank's in advance...

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why final?

    It is because you are using it in an anonymous inner class, the ActionListener, and only class fields or final local variables may be used inside of anonymous inner classes. I believe that the JVM makes copies of the variables used in the anonymous class, and to make sure that the value held by the variable does not change, it must be final. To see more on this, please check out this link.
    faraa likes this.

  3. #3
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    Thank you so much...
    I need to change Tank t in that anonymous inner class.
    What's your idea?

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why final?

    Quote Originally Posted by faraa View Post
    Thank you so much...
    I need to change Tank t in that anonymous inner class.
    What's your idea?
    Change t how? You can change the properties of the Tank object that t refers to just fine. You just can't re-assign t to a new Tank object, but you don't want to do that any way since that will have no effect on the original object.
    faraa likes this.

  5. #5
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    It is a game and I exactly should remove the Tank after a Time.and add it again(in a new place)

  6. #6
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    Oh , you're right I can just take the tank in new place without remove and add again
    Sorry and thank's...
    :D :D :D

  7. #7
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    Sorry again.
    When I can restart the timer in this program?
    (timer should restart after changing the tank place)

    Java Code:
     public void TimerSet(final Tank t){
            int delay=1000*10;
             Timer timer;
            ActionListener task=new ActionListener() {
    
                @Override
                public void actionPerformed(ActionEvent ae) {
                    numTank--;
                    t.x=parent.getWidth() / 2 - 50;
                    t.y=parent.getHeight() -200;
                    wd.setMid();
                    //timer.restart();
                        }
                    
                
            };
            timer=new  Timer(delay,task);
            timer.start();
        }
    Thank's in advance.

  8. #8
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why final?

    Quote Originally Posted by faraa View Post
    Sorry again.
    When I can restart the timer in this program?
    (timer should restart after changing the tank place)
    Perhaps it's me, but your question is overly vague and ambiguous for me, too much so to allow me to give a decent answer, other than to suggest that perhaps your Timer variable should be a class field and not a local variable.
    faraa likes this.

  9. #9
    Join Date
    Jun 2013
    Posts
    4
    Rep Power
    0

    Default Re: Why final?

    I am new on this java forum. I have started creating my blog on java. You can view and comment
    link deleted
    Last edited by Fubarable; 06-22-2013 at 05:48 PM.

  10. #10
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why final?

    Quote Originally Posted by kumar.bhatia18 View Post
    I am new on this java forum. I have started creating my blog on java. You can view and comment
    ...
    Kumar: please do not hijack another persons thread with this type of post. This is considered forum spam and can get you banned.
    Last edited by Fubarable; 06-22-2013 at 05:48 PM.

  11. #11
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    Quote Originally Posted by Fubarable View Post
    Perhaps it's me, but your question is overly vague and ambiguous for me, too much so to allow me to give a decent answer, other than to suggest that perhaps your Timer variable should be a class field and not a local variable.
    Ok,Thank you again

  12. #12
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    Any Other Idea For Post #7 ?

  13. #13
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    I created a time class and put this code in it .
    But statements on it don't work
    Why?
    Java Code:
    public class Time {
        
        JApplet parent;
        WallDevastator wd;
        int numTank;
        
        
        public Time(JApplet parent,WallDevastator wd,int numTank){
            this.parent=parent;
            this.wd=wd;
            this.numTank=numTank;
            
        }
        
         public void TimerSet(final Tank t) {
            int delay = 1000 * 30;
            Timer timer;
            ActionListener task = new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent ae) {
                    numTank--;
                    t.x = parent.getWidth() / 2 - 50;
                    t.y = parent.getHeight() - 200;
                    wd.iw = (parent.getWidth() / 2 - 20) / 10;
                    wd.jw = (parent.getHeight() - 170) / 10;
                }
            };
           
          //  timer.restart();
        }
    
        
        
    }


    and in main class:
    Java Code:
    Time time=new Time(parent, wd, numTank);
                        time.TimerSet(t);
    I mean that tank.x,tank.y,numTank don't change.
    Can anybody help me?
    Thank's..

  14. #14
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    ???????????????????
    plz help me....

  15. #15
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why final?

    Please clarify your current problem. What exactly isn't working with your current code? Where exactly are you stuck?

    For instance, this statement,
    But statements on it don't work...
    is very ambiguous (at least to me). When you run into such problems, please tell us what exactly isn't working, how it doesn't work, what errors, if any, you might be seeing.
    Last edited by Fubarable; 06-23-2013 at 01:59 PM.
    faraa likes this.

  16. #16
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    I mean that line21,22,23,24,25 don't work without any errors or exceptions...
    Thank's...

  17. #17
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default Re: Why final?

    Quote Originally Posted by faraa View Post
    I mean that line21,22,23,24,25 don't work without any errors or exceptions...
    Thank's...
    I don't know about the others, but I'm still stumped as to what your actual current problem is. I'll be glad to help if you're willing to put more effort into the question.

  18. #18
    faraa is offline Member
    Join Date
    Mar 2013
    Posts
    70
    Rep Power
    0

    Default Re: Why final?

    Ok,
    sorry.I don't understand what do you mean...
    Thank's....

Similar Threads

  1. Final int
    By Fireking in forum New To Java
    Replies: 12
    Last Post: 06-22-2013, 05:44 PM
  2. Replies: 1
    Last Post: 10-19-2011, 09:04 PM
  3. [SOLVED] is final class members are also final ?
    By haoberoi in forum New To Java
    Replies: 4
    Last Post: 11-10-2008, 03:01 PM
  4. Poi 3.0-final
    By levent in forum Java Software
    Replies: 0
    Last Post: 05-22-2007, 07:05 AM

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •