Results 1 to 10 of 10

Thread: new keyword?

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

    Default new keyword?

    Why do people use new keyword inside of method parameters? I don't understand why people simple say
    Java Code:
    new ActionListener()
    They don't make an object... Why do they do this, and make a nameless object? I don't understand... does that just like make the constructor run? Why do people make a statement saying
    Java Code:
    new RandomClass()
    Thanks all

  2. #2
    monkeyjr97 is offline Senior Member
    Join Date
    Nov 2012
    Posts
    258
    Rep Power
    2

    Default Re: new keyword?

    it does create a new Object. Everything in Java in as Object.

    The new keyword creates instances of said type, this can be a Class or an Object or a data structure like ArrayList
    Java Code:
    e.g. ArrayList<X>a = new ArrayList<X>()
    it also allows you to have several instances of the same Object, but these instances can have different values, and different memory locations

    Java Code:
    Person a = new Person();
    Person b = new Person();
    Person c = new Person();
    
    a.setName("ken");
    b.setName("dave");
    c.setName("gary");
    In your application if you didnt create instances of the objects you need, you will get nullPointerExceptions.

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: new keyword?

    Sometimes you don't need a reference to an object so you just create the object as you described. A typical example would be adding some event listener.
    Java Code:
    button.addActionListener(new MyActionListener());
    Regards,
    Jim
    The Java™ Tutorial | SSCCE | Java Naming Conventions
    Poor planning our your part does not constitute an emergency on my part.

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

    Default Re: new keyword?

    I'm not quite grasping this. What is the point of creating an object if not to refer to it? What exactly does an "instance" mean... I get what an object is, but not really why you just say new ActionListener, and don't create a reference. I don't get what that does. Ugh, thanks for all replies

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: new keyword?

    jim's not quite correct.
    It does create a reference in his example.
    But in that case the reference is only actually needed in that method being called, so no point assigning it to any other variable.
    That reference is passed in to the method and assigned to the parameter.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: new keyword?

    Yeah! Poor wording on my part.

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

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

    Default Re: new keyword?

    but guys, what is the point of creating an object that cannot even be referenced :(?

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,949
    Rep Power
    19

    Default Re: new keyword?

    It is referenced.
    In the example of the action listener the reference is passed into the method...it's one of the parameters.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    JosAH's Avatar
    JosAH is online now Moderator
    Join Date
    Sep 2008
    Location
    Voorschoten, the Netherlands
    Posts
    13,447
    Blog Entries
    7
    Rep Power
    20

    Default Re: new keyword?

    Quote Originally Posted by monkeyjr97 View Post
    it does create a new Object. Everything in Java in as Object.
    The new operator has to do a bit more: it has to load the class (and possibly its superclass(es)) if not already done so; finally it allocates memory for the new object, clears it and calls the constructor mentioned to the right of the new operator; The implicit 'this' pointer points to the allocated memory.

    kind regards,

    Jos
    cenosillicaphobia: the fear for an empty beer glass

  10. #10
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,524
    Rep Power
    5

    Default Re: new keyword?

    In my earlier gaffe I omitted the words "directly accessible" to describe the reference. The following two constructs accomplish the same thing:

    Java Code:
    panel.addActionListener(new MyActionListener());
    
    // or
    
    ActionListener al = new MyActionListener();
    panel.addActionListener(al);
    The choice above depends on personal preference or on how many different components are listening for events. If one action listener is processing events for mulitple components, then the second form above may be preferred as it allows the same reference to be used in subsequent addActionListener calls.

    One more example of not needing a direct reference to an object is passing a Comparator instance to Collections.sort(). The sort routine needs it to perform the sort.

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

Similar Threads

  1. The this keyword
    By Army in forum New To Java
    Replies: 15
    Last Post: 12-14-2012, 04:01 AM
  2. Using 'this' keyword in constructor
    By kudwn in forum New To Java
    Replies: 2
    Last Post: 10-11-2010, 12:12 PM
  3. Use of keyword instanceof
    By darek9576 in forum New To Java
    Replies: 3
    Last Post: 03-14-2010, 10:35 PM
  4. this keyword
    By coltragon in forum New To Java
    Replies: 10
    Last Post: 03-01-2010, 09:20 AM
  5. Use of this keyword
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 11-18-2007, 07:32 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
  •