Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 5 of 5

Thread: Accessing static method

  1. #1
    Junior Member
    Join Date
    Feb 2011
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Question Accessing static method

    Bit of a newbie question but this has always baffled me and I have not been able to get a simple answer.

    Just wonder why Netbeans moans about this code

    frame = new JFrame();
            frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
            frame.setDefaultLookAndFeelDecorated(true);


    Why does it complain about accessing a static method?

    When I ask it to change it, it does this:

    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    JFrame.setDefaultLookAndFeelDecorated(true);

    Which does not look right to me? (How would java know I am referring to the frame I am creating and not just frames in general).

    I also note that net beans does not say anything when I do this

    frame = new JFrame();
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOS E);
    setDefaultLookAndFeelDecorated(true);

    So which is correct here?
    Last edited by argolian; February 16th, 2011 at 05:47 AM.


  2. #2
    Forum Squatter newbie's Avatar
    Join Date
    Nov 2010
    Location
    North Wales
    Posts
    661
    My Mood
    Stressed
    Thanks
    28
    Thanked 115 Times in 106 Posts
    Blog Entries
    1

    Default Re: Accessing static method

    Hmm, I can't seem to spot the difference between any of the code you posted
    Is your JFrame frame; declared outside of the main method?
    Please use [highlight=Java]//code goes here...[/highlight] tags when posting your code

  3. #3
    Administrator copeg's Avatar
    Join Date
    Oct 2009
    Location
    US
    Posts
    5,332
    Thanks
    181
    Thanked 829 Times in 772 Posts
    Blog Entries
    5

    Default Re: Accessing static method

    frame.setDefaultLookAndFeelDecorated(true);

    The above code is accessing a static method from an defined instance. It will still compile, but you will receive a warning. You should listen to your compiler, access static methods the correct way...by using the class name followed by the static variable/method.
    JFrame.setDefaultLookAndFeelDecorated(true);

  4. #4
    Junior Member
    Join Date
    Feb 2011
    Posts
    4
    Thanks
    0
    Thanked 1 Time in 1 Post

    Default Re: Accessing static method

    Bear in mind that we're talking about defaults, so it is ok to set these for frames in general (until you say otherwise).

    If you check the reference, indeed setDefaultLookAndFeelDecorated() is static and setDefaultCloseOperation() isn't. Not sure why, but it is the case:
    Java Platform SE 6

    Your import javax.swing.JFrame; has to be what allows you to leave off the JFrame part. It wasn't completely clear to me that that always works when you have the import statement, but I'd say that's gotta be the reason why NetBeans doesn't complain.

  5. #5
    Member DavidFongs's Avatar
    Join Date
    Oct 2010
    Location
    Minneapolis, MN
    Posts
    107
    Thanks
    1
    Thanked 45 Times in 41 Posts

    Default Re: Accessing static method

    Quote Originally Posted by aisthesis View Post
    Your import javax.swing.JFrame; has to be what allows you to leave off the JFrame part. It wasn't completely clear to me that that always works when you have the import statement, but I'd say that's gotta be the reason why NetBeans doesn't complain.
    This is not true. Would have to see all of his code to know the reason. I would guess that the object that contains the method, that contains that code, is extending JFrame (or extending something that is extending JFrame).

Similar Threads

  1. non-static method cannot be referenced from a static context
    By Kaltonse in forum What's Wrong With My Code?
    Replies: 2
    Last Post: December 21st, 2010, 06:51 PM
  2. Error using public static double Method
    By stommy989 in forum What's Wrong With My Code?
    Replies: 5
    Last Post: October 13th, 2010, 03:01 PM
  3. Replies: 10
    Last Post: September 6th, 2010, 04:48 PM
  4. [SOLVED] static variable in an instance method?
    By chronoz13 in forum Java Theory & Questions
    Replies: 2
    Last Post: January 30th, 2010, 02:24 AM
  5. Static method
    By kalees in forum What's Wrong With My Code?
    Replies: 4
    Last Post: November 20th, 2009, 10:10 AM