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

    Default Question about objects as parameters, and the new keyword

    When you have objects as a parameter such as catch(Exception e), or paint(Graphics g), why do you just give the object a random name. Also, why doesn't it have to be instantiated? Another question about the new keyword.
    When you don't declare an object and just say new Apple(); , why do programmers do that? They don't make an object. They just say new Apple(); I've looked through tutorials. None of them explain this. Thanks all

  2. #2
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Question about objects as parameters, and the new keyword

    Parameters are just like declaring a variable. The acceptable practice is to give them meaningful names but many people tend to give parameters abbreviated names especially if they will only be used temporarily.

    Using new Foo() is creating an object despite what you think. The only difference is that the reference returned is not assigned to a variable. Consider the following code (try running it to see what happens).
    Java Code:
    class Foo {
        Foo() {
            run();
        }
    
        public void run() {
            System.out.println("Hello World");
        }
    
        public void runAnother() {
            System.out.println("Another World");
        }
    
        public static void main(String[] args) {
            new Foo();
            new Foo().runAnother();
        }
    }

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

    Default Re: Question about objects as parameters, and the new keyword

    Quote Originally Posted by Junky View Post
    Parameters are just like declaring a variable. The acceptable practice is to give them meaningful names but many people tend to give parameters abbreviated names especially if they will only be used temporarily.

    Using new Foo() is creating an object despite what you think. The only difference is that the reference returned is not assigned to a variable. Consider the following code (try running it to see what happens).
    Java Code:
    class Foo {
        Foo() {
            run();
        }
    
        public void run() {
            System.out.println("Hello World");
        }
    
        public void runAnother() {
            System.out.println("Another World");
        }
    
        public static void main(String[] args) {
            new Foo();
            new Foo().runAnother();
        }
    }
    Great reply! However, why do people create the object, and not name it? What is the reason?

  4. #4
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,755
    Rep Power
    7

    Default Re: Question about objects as parameters, and the new keyword

    I just provided an example. You can just create an object in the main method to kick off the program. All the rest of the logic is contained in the created object.

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

    Default Re: Question about objects as parameters, and the new keyword

    I'm a fan of self documenting variables, including variables introduced as parameters. Most IDEs helpfully pop these up on the screen, and if the parameters are nicely named you know the order without having to go to the trouble of reading the docs. A parameter like w for width is all very well ... until the day when it means "weight".

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

    Default Re: Question about objects as parameters, and the new keyword

    are you referring to the (Exception e) in catch methods etc because those Exceptions are defined elsewhere within the Java language itself so you dot have to instantiate them. It just letting the compiler and and the system know what to look for and what to catch. The reason most people call exception e is because it makes sense really, and it saves a bit of time.

    for example if i had an actionEvent handler like

    Java Code:
            public void keyPressed(KeyEvent e) {
    
                int key = e.getKeyCode();
    i could just say e.getKeyCode(); etc. if i called my exception execptionToBeCaught (extereme example i known) then id have to write exceptionToBeCaught.getKeyCode(); which would get very annoying if i had to keep typing this over and over.

Similar Threads

  1. Replies: 12
    Last Post: 04-29-2012, 02:55 PM
  2. objects as parameters change in method
    By zcd in forum New To Java
    Replies: 0
    Last Post: 01-29-2012, 08:37 PM
  3. Question about generic parameters
    By Dirty Protest in forum New To Java
    Replies: 3
    Last Post: 07-01-2011, 06:19 AM
  4. Replies: 1
    Last Post: 02-03-2011, 05:43 PM
  5. Replies: 3
    Last Post: 09-29-2010, 02:38 PM

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
  •