Results 1 to 7 of 7
  1. #1
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Initializing a constructor as the parameter for a method? Very confused

    So, I am learning Swing, and Swing Layouts. I just came across a really confusing (for me) line of code.
    Java Code:
    setLayout(new BorderLayout());
    So, this is weird for me because I don't really understand why the BorderLayout class constructor is being initialized as a parameter for the setLayout.. can someone please explain?

  2. #2
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Initializing a constructor as the parameter for a method? Very confused

    It is a short way of doing this:

    Java Code:
    BorderLayout b = new BorderLayout();
    setLayout(b);
    You can see that my code does the same thing as your code, yours is just shorter. The only restriction for your code, is you can't directly alter the BorderLayout.
    Yours also saves the computer time because you never assign RAM for the variable b. (very little time)
    Last edited by zFollette; 01-02-2014 at 09:18 AM.

  3. #3
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Initializing a constructor as the parameter for a method? Very confused

    Oh, that actually really helped! The only thing I am still unclear about is why there has to be a BorderLayout Object and you can't do something along the lines of
    Java Code:
    setLayout(BorderLayout);
    Thanks for the reply though!

  4. #4
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Initializing a constructor as the parameter for a method? Very confused

    That code (if it was valid) would only tell java to use a layout called BorderLayout, it would never give java an actual reference to the BorderLayout class, unless you did something like this:

    Java Code:
    BorderLayout BorderLayout = new BorderLayout();
    setLayout(BorderLayout);
    Which is valid, but not recommended due to Java Naming Conventions.

  5. #5
    pj6444 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    165
    Rep Power
    2

    Default Re: Initializing a constructor as the parameter for a method? Very confused

    If you only used BorderLayout instead of new BorderLayout(), you would not be giving the constructor an object of the BorderLayout class. This would result in an error because the setLayout() method requires an object of a border class.

  6. #6
    MW130 is offline Senior Member
    Join Date
    Jan 2013
    Posts
    317
    Rep Power
    2

    Default Re: Initializing a constructor as the parameter for a method? Very confused

    Oh, so it requires an object because that object would "in a way" give it access to everything within that class? I get what you guys are saying, but am not clear as to why it need an object of the BorderLayout class. Thanks

  7. #7
    zFollette is offline ▼ dafuq did I do?
    Join Date
    Dec 2013
    Posts
    203
    Rep Power
    0

    Default Re: Initializing a constructor as the parameter for a method? Very confused

    Quote Originally Posted by MW130 View Post
    Oh, so it requires an object because that object would "in a way" give it access to everything within that class? I get what you guys are saying, but am not clear as to why it need an object of the BorderLayout class. Thanks
    Within the Object BorderLayout, there is a set of methods that control the layout of the frame it is in. If you don't point the layout to this object, even if it was valid code, nothing would happen.

Similar Threads

  1. Context parameter for an Intent constructor
    By Nazneen Ali in forum Android
    Replies: 2
    Last Post: 11-02-2013, 08:17 PM
  2. initializing InetAddress object in constructor
    By sixxvirus in forum Networking
    Replies: 1
    Last Post: 05-20-2012, 07:23 AM
  3. Initializing a variable from another method
    By erikjd21 in forum New To Java
    Replies: 4
    Last Post: 04-17-2011, 09:55 AM
  4. So Confused (array/arrayList in constructor)
    By thesinter in forum New To Java
    Replies: 3
    Last Post: 12-01-2009, 10:53 PM
  5. Replies: 2
    Last Post: 12-19-2008, 03:20 AM

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
  •