Results 1 to 5 of 5
  1. #1
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default A question on JFrame constructor.

    I have a class:

    Java Code:
    import javax.swing.*;
    public class SamsLearning extends JFrame {
    	public SamsLearning() {
    		this.setTitle("My Title");
    		this.setSize(300,300);
    		this.setVisible(true);
    	}//end constructor
    }//end class SamsLearning
    In my Test Class, in my main method when I create an object of class SamsLearning I get a nice visible frame.

    My questions is:

    What is the difference between calling super() in my consturctor?

    This is from the API:

    JFrame

    public JFrame()
    throws HeadlessException
    Constructs a new frame that is initially invisible.
    This constructor sets the component's locale property to the value returned by JComponent.getDefaultLocale.

    Is the only difference that I get the exception available in my code ?

    Thank you.

  2. #2
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default Re: A question on JFrame constructor.

    I believe that super() will be called implicitly if you don't call it explicitly.

  3. #3
    fatabass is offline Senior Member
    Join Date
    Nov 2011
    Location
    Turkey
    Posts
    380
    Blog Entries
    24
    Rep Power
    3

    Default Re: A question on JFrame constructor.

    Can someone verify sunde887 ?

  4. #4
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A question on JFrame constructor.

    Quote Originally Posted by fatabass View Post
    Can someone verify sunde887 ?
    JLS 8.8.7 "If a constructor body does not begin with an explicit constructor invocation and the constructor being declared is not part of the primordial class Object, then the constructor body is implicitly assumed by the compiler to begin with a superclass constructor invocation "super();", an invocation of the constructor of its direct superclass that takes no arguments."

    So consider it verified (except to mention that in the case of an enum type, you can't call it explicitly.)

    -----

    And, yes, if you call it explicitly you can catch the exception and not otherwise. It is unlikely that you want to catch this exception however. See JLS 8.8.7.1 for the gruesome detail on explicit superclass constructor invocation.

  5. #5
    pbrockway2 is offline Moderator
    Join Date
    Feb 2009
    Location
    New Zealand
    Posts
    4,565
    Rep Power
    12

    Default Re: A question on JFrame constructor.

    Also relevant are jverd's c'tor rules. Quoted in the third post here.

Similar Threads

  1. Head First Java book-Constructor question
    By silverglade in forum New To Java
    Replies: 28
    Last Post: 05-05-2011, 04:29 PM
  2. constructor question
    By mac in forum New To Java
    Replies: 4
    Last Post: 03-20-2010, 09:22 PM
  3. Java Constructor Question
    By ashly in forum New To Java
    Replies: 6
    Last Post: 03-09-2010, 03:10 AM
  4. JFrame Question ?
    By Mindhunter74 in forum New To Java
    Replies: 2
    Last Post: 12-21-2007, 11:45 PM
  5. JFrame question
    By Try2Live4God in forum New To Java
    Replies: 2
    Last Post: 10-15-2007, 02:52 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
  •