Results 1 to 9 of 9
  1. #1
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default Understanding -- new Class() -- syntax.

    Java Code:
    ...
    FlowLayout testLayout = new FlowLayout();
    setLayout(test);
    ...
    What is the point of setting testLayout to equal FlowLayout? It seems to me now we have two classes FlowLayout and now testLayout that are both the same, isn't that counter productive? I see this, "Class newClass = new Class()", type of syntax a lot in Java programming.

    Why wouldn't Java do setLayout.FlowLayout(); or something like that? What is it that I am missing here?


    I am hoping the answer to this can help me better understand Java. I bought a book Sams Teach Yourself Java, and it is pretty good. I am on hour 14 out of 25, but still have questions. I intend on getting very good at Java and understanding it very well.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    FlowLayout is the class and test is simply a variable that refers to a FlowLayout object. There's nothing counter productive here.

    Why wouldn't Java do setLayout.FlowLayout(); or something like that? What is it that I am missing here?
    This doesn't make sense. setLayout is a method and it takes a layout object as its parameter. You could do:
    Java Code:
    setLayout(new FlowLayout());

    But this is pretty much equivalent to what you already have:


    Java Code:
    FlowLayout test = new FlowLayout();
    setLayout(test);

  3. #3
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    I think I need to read more on methods classes and objects. So far I am still confused on this stuff.

    I feel dumb for asking the questions now /:

    Sometimes in these explanations, even in beginner books, it is explained in a way that I just don't get it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Think of a class like a blue print for creating something. So FlowLayout is not a thing but more a set of instructions. Think of an object as a thing, something that you can use. So you create a FlowLayout object by calling the constructor with new FlowLayout(). Think of the variable, such as "test", as the address or name or location of an object, some way to get a handle on the object. Since you need a FlowLayout object to be made and used in the setLayout(...) method you need to call new FlowLayout somewhere, be it in the method parameter itself, or to set test to refer to the object.

  5. #5
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    So I guess if I rephrase it.

    What benefit do you have in doing example 1 vs 2? Is it just personal preference?

    Example 1...
    Java Code:
    FlowLayout flo = new FlowLayout();
    setLayout(flo);
    Example 2...
    Java Code:
    setLayout(new FlowLayout());

  6. #6
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,315
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by AcousticBruce View Post
    So I guess if I rephrase it.

    What benefit do you have in doing example 1 vs 2? Is it just personal preference?

    Example 1...
    Java Code:
    FlowLayout flo = new FlowLayout();
    setLayout(flo);
    Example 2...
    Java Code:
    setLayout(new FlowLayout());
    My guess is that there really is little difference between the two, that the ultimate instructions generated by the JVM will either be the same or very similar. I would go with whichever seems more clear to you.

  7. #7
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    The difference between the two examples is that in Example 1 you have a reference to your FlowLayout object called flo. This may come in handy if you need to make changes to or call methods of the FlowLayout object later on. Sometimes there are other ways of getting a reference to the object again, like a getLayout() method. I don't know Swing very thoroughly, but I don't think JFrame has a getLayout() method, for instance.

    So to review:

    FlowLayout is the class. You could use it directly to get access to the class's static fields, such as FlowLayout.LEFT, FlowLayout.CENTER, etc.

    FlowLayout() is the constructor -- specifically the default constructor (no args). There are two other constructors available, in case you want to specify alignment and horizontal and vertical gaps when you are creating your object.

    testLayout in your initial example is a FlowLayout object. That's not the same as the class itself -- it's an instance of the class. There might even be other FlowLayout objects in the same program, and they are certainly not all the same.

    Hope that helps.

    -Gary-

  8. #8
    AcousticBruce is offline Senior Member
    Join Date
    Dec 2010
    Location
    Indiana
    Posts
    202
    Rep Power
    5

    Default

    I think I am now getting it.

    Java Code:
    GridLayout grid1 = new GridLayout(5,5);
    and
    Java Code:
    GridLayout grid2 = new GridLayout(2,8);

    So now I can call grid1 or grid2 because they contain arguments within the method. Is my word choice and idea correct?

  9. #9
    gcalvin is offline Senior Member
    Join Date
    Mar 2010
    Posts
    952
    Rep Power
    5

    Default

    Quote Originally Posted by AcousticBruce View Post
    I think I am now getting it.

    Java Code:
    GridLayout grid1 = new GridLayout(5,5);
    and
    Java Code:
    GridLayout grid2 = new GridLayout(2,8);

    So now I can call grid1 or grid2 because they contain arguments within the method. Is my word choice and idea correct?
    Well, you have instantiated two different GridLayout objects. It doesn't really matter so much that their constructors took arguments.
    Java Code:
            Dog rex = new Dog();
            Dog rover = new Dog();
    rex is not rover, but rex and rover are both Dogs.

    -Gary-

Similar Threads

  1. need help in understanding collection
    By ShinTec in forum Advanced Java
    Replies: 2
    Last Post: 04-24-2010, 03:49 AM
  2. Help on understanding a program
    By newbie225 in forum New To Java
    Replies: 1
    Last Post: 11-10-2009, 01:53 AM
  3. Having trouble understanding Class Graphic
    By Bernard Robitaille in forum JCreator
    Replies: 1
    Last Post: 04-18-2009, 03:55 AM
  4. Replies: 3
    Last Post: 10-07-2008, 12:03 AM
  5. Understanding Vectors
    By cbrown08 in forum New To Java
    Replies: 7
    Last Post: 11-05-2007, 07:56 PM

Posting Permissions

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