Results 1 to 10 of 10
  1. #1
    javnam is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Default super and this problem

    hi all
    there is a problem in " super.name "
    i tried to use this.name and also problem
    please help me

    Java Code:
    		ActionListener actionListener = new ActionListener(){
    			public void actionPerformed(ActionEvent actionEvent){
    				tfName.setText(super.name);
    			}
    		};

  2. #2
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: super and this problem

    super.name is a legal construct though it is probably not doing what you expect. Whether you can do it or not depends on where your actionListener is defined. If it is defined in an instance method and "name" is an instance variable, then it will work. If the listener is defined in a static method then it won't work unless "name" is qualified with a proper class reference. If "name" is a local variable in the same method where the actionListener is created then it won't work unless "name" is declared final.

    If you provide more code, then I will be better able to help you.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  3. #3
    javnam is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Default Re: super and this problem

    thank you sir
    iam asking for the best way
    i have a main class with static void main()
    i have a class have members " Vars.class "
    i have a form class " FormInsert.class "
    i make extends from " Vars.class " to use their variables by super
    and use the variables inside actionListener
    is this a good way ?
    do you sir have another way?

  4. #4
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    United States
    Posts
    2,924
    Rep Power
    4

    Default Re: super and this problem

    Here is one way to do it.

    Java Code:
    public class Foo  {
       private String  name = "foo";
       private JButton button = new JButton();
    
       public static void main(String[] args) {
          new Foo().init();
       }
    
       public void init() {
          ActionListener actionListener = new ActionListener() {
             public void actionPerformed(ActionEvent actionEvent) {
                button.setText(name);
             }
          };
          button.addActionListener(actionListener);
       }
    }
    I would never use super to access a super class instance field. If the class is properly designed you shouldn't be able to do that anyway.

    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

  5. #5
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: super and this problem

    I'm not sure I've understood this question, but jim829 is correct to an extent -- from my understanding, javnam have a separate class where he is storing his constants named Vars.class -- You can easily access these by implementing that class as an 'interface' and can be used in other classes by:

    Java Code:
    public SomeClass implements Vars
    Apologies if I've misunderstood, but that what I gathered from the 2nd post by javnam after the original question.

    Edit:
    -----

    Here's an example of what I'm talking about.

    Vars.class
    Java Code:
    public interface Vars
    {
        public static String SOME_STRING_A = "This is some string A!";
        public static String SOME_STRING_B = "This is some string B!";
        public static String SOME_STRING_C = "This is some string C!";
        public static String SOME_STRING_D = "This is some string D!";
    }
    SomeClass.class
    Java Code:
    public class SomeClass implements Vars
    {
        public static void main(String[] args)
        {
            System.out.println(SOME_STRING_A);
            System.out.println(SOME_STRING_B);
            System.out.println(SOME_STRING_C);
            System.out.println(SOME_STRING_D);
        }
    }
    Last edited by sevoxx; 04-28-2013 at 12:55 AM.

  6. #6
    javnam is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Default Re: super and this problem

    jim829 and sevoxx thanks a lot for you
    i post a simple project
    there is a problem
    could you please solve it ?
    Attached Files Attached Files

  7. #7
    sevoxx is offline Oracle Java Developer
    Join Date
    Apr 2013
    Location
    In a PineApple Under the Sea
    Posts
    20
    Rep Power
    0

    Default Re: super and this problem

    It helps if you state what the problem is.

    Edit:
    -----

    Do you catch any Exceptions? Have you tried making use of your good old friend System.out.println()? Please help us help you by providing some information -- I doubt anyone would be too keen on grabbing someones project, patching it up and pushing back. That's not the way things are done.
    Last edited by sevoxx; 04-29-2013 at 12:12 AM.

  8. #8
    javnam is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Default Re: super and this problem

    I am just asking, I am a newbie
    ok i will search for answer my self
    Last edited by javnam; 04-30-2013 at 12:12 PM.

  9. #9
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,442
    Rep Power
    18

    Default Re: super and this problem

    First off, people generally won't download attachments.
    So you're likely to get a better answer by postnig the code here.

    Second, don't just dump all your code here.
    If you have a problem, narrow it down and post the relevant code.

    And post any compilation messages, highlighting the line(s) they occur on.
    If it runs, then post any exceptions, complete with stack trace, highlighting the line in your code it is thrown from.
    Please do not ask for code as refusal often offends.

  10. #10
    javnam is offline Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Default Re: super and this problem

    Thank you Tolls I'll take your words into consideration in the coming times

Similar Threads

  1. I am a super noob with a super noob question.
    By LittleZoppo in forum Java Applets
    Replies: 3
    Last Post: 04-27-2012, 03:50 AM
  2. super
    By diamonddragon in forum New To Java
    Replies: 3
    Last Post: 01-29-2012, 10:49 PM
  3. problem in set & get method of a super class
    By elenora in forum Advanced Java
    Replies: 5
    Last Post: 04-22-2011, 01:42 PM
  4. calling variable using super super..
    By Stephen Douglas in forum New To Java
    Replies: 7
    Last Post: 08-16-2010, 06:12 AM
  5. problem with super.tostring()
    By faw in forum Advanced Java
    Replies: 1
    Last Post: 05-01-2008, 04:27 PM

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
  •