Results 1 to 8 of 8

Thread: Name Conflict

  1. #1
    vanhuh21 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default Name Conflict

    This is my first time posting it. Any help is greatly apprecaited.

    I have this problem of a name conflict. I will only write codes that is relevant to my problem. so please assume that there is other codes there.


    in MyJava.java

    // Constructor

    Java Code:
    private JList userList;
    private TabbedPane tabbedPane; //this is a class that extends JTabbedPane class.
    
    public MyJava(JTabbedPane tabbedPane)
    {
    
       this.tabbedPane = tabbedPane;
       userList = new JList(userNames.toArray());
    
       userList.addListSelectionListener(new ListSelectionListener(){
    			public void valueChanged(ListSelectionEvent e){
    				
         //This is where I am getting errors.     
     				
        JTextField userNameField = tabbedPane.getUserNameField();
        JTextField addressField = tabbedPane.getAddressField();
         JTextField emailField = tabbedPane.getEmailField();
    			
       }
    }
    when I change the name "tabbedPane" to a different name, then it is referencing the private attribute as I intended.

    However, if I keep the name same for the one passed in and the one on the top, I get the red line under the tabbedPane.getUserNameField();

    I kind of do not understand this because I was allowed to do

    this.tabbedPane. = tabbedPane


    tabbedPane that I want to use is of the private attribute i created on the top.

    any input is greatly appreciated

    Thanks
    Last edited by Eranga; 08-08-2010 at 01:33 PM. Reason: code tags added

  2. #2
    Zack's Avatar
    Zack is offline Senior Member
    Join Date
    Jun 2010
    Location
    Destiny Islands
    Posts
    692
    Rep Power
    5

    Default

    Without seeing your exact error message it is hard to tell. However, I can guess that the problem is that the compiler thinks you're trying to reference the local variable tabbedPane rather than the private attribute. The solution seems to be to change one of their names.

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    Quote Originally Posted by Zack View Post
    However, I can guess that the problem is that the compiler thinks you're trying to reference the local variable tabbedPane rather than the private attribute. The solution seems to be to change one of their names.
    Yeah this could be the reason, OP says that he has more than what he posts in his code.

  4. #4
    vanhuh21 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default Thanks

    I think that's what exactly is hapenning.

    but what I do not understand is that if I put "this" in front of the variable, would it automatically reference the private attribute?

    Here it's not happening that.

  5. #5
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    What error message do you get when you compile it?
    I don't use an IDE with red lines.

    Do you understand there are two variables with the same name? The inner anonymous class could be looking at the closer defined one local to the method vs the class member. That would be a problem because the one local to the method is a param and would go away when the method exited. What happens if you make the param final?

  6. #6
    vanhuh21 is offline Member
    Join Date
    Aug 2010
    Posts
    3
    Rep Power
    0

    Default

    Inserting "final" in front of the parameter has solved the problem.

    like Public MyJava( final JTabbedPane tabbedpane). Thanks for help.

    I still do not understand why it fixed the problem . can you explain to me?

  7. #7
    Norm's Avatar
    Norm is offline Moderator
    Join Date
    Jun 2008
    Location
    SW Missouri
    Posts
    17,387
    Rep Power
    25

    Default

    problem because the one local to the method is a param and would go away when the method exited.
    To be able to use the value of that param later, the compiler must do something to save it so it is available when the anonymous class's method needs it. If the variable were local, it goes out of scope when the method exits. final tells the compiler to save it.

  8. #8
    Tolls is online now Moderator
    Join Date
    Apr 2009
    Posts
    12,001
    Rep Power
    20

    Default

    Quote Originally Posted by vanhuh21 View Post
    Java Code:
    private JList userList;
    private TabbedPane tabbedPane; //this is a class that extends JTabbedPane class.
    
    public MyJava(JTabbedPane tabbedPane)
    {
    
       this.tabbedPane = tabbedPane;
       userList = new JList(userNames.toArray());
    
       userList.addListSelectionListener(new ListSelectionListener(){
    			public void valueChanged(ListSelectionEvent e){
    				
         //This is where I am getting errors.     
     				
        JTextField userNameField = tabbedPane.getUserNameField();
        JTextField addressField = tabbedPane.getAddressField();
         JTextField emailField = tabbedPane.getEmailField();
    			
       }
    }
    when I change the name "tabbedPane" to a different name, then it is referencing the private attribute as I intended.

    However, if I keep the name same for the one passed in and the one on the top, I get the red line under the tabbedPane.getUserNameField();
    Variables used inside an anonymous inner class (which is what the ListSelectionListener you've created is) have to either be a member of the parent class (in this case userList or tabbedPane) or be "final". The reason for this is a non-final local variable may change its reference before the listener gets called, and the "final" prevents this from happening (the compiler won't let it through). If it didn't do this it would be possible to do:

    Java Code:
    public MyJava(JTabbedPane tabbedPane)
    {
       userList.addListSelectionListener(new ListSelectionListener(){
    			public void valueChanged(ListSelectionEvent e){
    				
        JTextField userNameField = tabbedPane.getUserNameField();
        JTextField addressField = tabbedPane.getAddressField();
         JTextField emailField = tabbedPane.getEmailField();
    			
       }
        tabbedPane = new JTabbedPane();
    }
    Now, which version of tabbedPane should be used when the ListSelectionListener is fired?

    In your case I'd just use this.tabbedPane, since it's the attribute and not the parameter you want to be using. Or change the name of the parameter.

    ETA: By "(v)ariables used inside an anonymous inner class" I mean ones declared outside the class, but used inside it.

Similar Threads

  1. jdk jre tomcat conflict
    By simo_mon in forum Java Servlet
    Replies: 7
    Last Post: 09-29-2009, 08:44 AM
  2. Replies: 6
    Last Post: 04-08-2009, 04:05 AM

Posting Permissions

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