aspose file tools*
The moose likes Java in General and the fly likes strange problem with final keyword Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "strange problem with final keyword " Watch "strange problem with final keyword " New topic
Author

strange problem with final keyword

naved momin
Ranch Hand

Joined: Jul 03, 2011
Posts: 692

i am facing a strange problem in my desktop app let me explain you that
i have a a frame at the start up which has just one button
and this buttons action performed method calls another frame say login panel frame
like

now the login panel class contains a constructor that is calling two method and the methods are
setGui(); //this one creates the object of each component
getGui(); // this one add them in panel and panel in frame.

now this i mean Login panel class constructor shows me a hint and that hint says make this class (login panel) final , this was just a hint not any compile time warning
so as soon as i mark this class as final
the problem i was facing just disappears
the problem was :
when i click on the start up frame's button (remember the button which calls the Login panel class ) it takes 5 to 6sec to bring the next frame
but after marking that (log in panel class) final this problem was all resolve
can you tell what the hell is happening
sorry i cant post the code here because the code is dam long
can any one explain me please ??? i need to learn what went wrong when that class was not final ..???


The Only way to learn is ...........do!
Visit my blog http://inaved-momin.blogspot.com/
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Please watch the punctuation and capital letters; that post is surprisingly hard to read!

The compiler says you must mark a local variable final before using it in an anonymous class? Is that the problem? That question comes up quite frequently.
Let’s see if I can remember what happens.
  • 1. You enter the method.
  • 2. An object of the anonymous class is created.
  • 3. That object needs the local variable.
  • 4. That object will continue to exist after the method has completed.
  • 5. That object takes a copy of the variable and stores it to use after the method has completed.
  • 6. That local variable is assigned to as the method runs.
  • 7. The local variable has one value (new) and the anonymous class object has an old, different value
  • The compiler cannot check that the variable is or is not assigned to. That is too complicated. So it insists the local variable be marked final.
    This problem does not occur with fields, because the lifetime of a field is the same as the anonymous class.
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    If you are using an IDE, it may have an option selected to mark as many fields as possible final. That is a useful design principle.
    naved momin
    Ranch Hand

    Joined: Jul 03, 2011
    Posts: 692

    Campbell Ritchie wrote:If you are using an IDE, it may have an option selected to mark as many fields as possible final. That is a useful design principle.


    i suppose you have not understand what i wanna say

    first the compiler didnt gave me any hint , it was the IDE and it is all ok for the compiler to compile the code even if i dont follow the hint of IDE

  • 1 display class which has a button when that button is clicked a class called as login panel starts



  • 2 login panel has 2 method ...method.1 setGui() ..which does nothing but create object of each component used in this class method 2 getGui() which add each component to the frame



  • 3 but after marking the class login panel as final solves my problem
  • 4 problem was : button which is on the display class was taking much time to bring the log in panel frame



  • hope you understand now ?
    Campbell Ritchie
    Sheriff

    Joined: Oct 13, 2005
    Posts: 39436
        
      28
    As a general design principle methods called from the constructor should have private access or be marked final. In the case of a setUpGUI method, it is probably better to have private access, because you don’t want that method to be called from anywhere else.
    If you have a public method which is overridden in a subclass, your constructor may behave differently and not work as you expected. So you would mark a public method final to prevent overriding. If your panel class is final, then you cannot override any methods because you cannot extend from that class.
     
     
    subject: strange problem with final keyword